Skip to content

Instantly share code, notes, and snippets.

@taylorotwell
Last active March 9, 2018 16:36
Show Gist options
  • Save taylorotwell/ee2f782aec59aa53863fd09c8e47f304 to your computer and use it in GitHub Desktop.
Save taylorotwell/ee2f782aec59aa53863fd09c8e47f304 to your computer and use it in GitHub Desktop.
<?php
class Decorator
{
public function __construct($target)
{
$this->target = $target;
}
public function __call($method, $parameters)
{
return $this->target->{$method}(...$parameters);
}
}
class Target
{
}
class Consumer
{
public function doesWork(Target $target)
{
};
}
// Doesn't work becuase type-hints are a waste of time and lead to poorly coded, brittle systems... :)
$consumer->doesWork(new Decorator(new Target));
@nicholasruunu
Copy link

A decorator need to implement the same interface as the origin.
The problem with __call (among many) is that it can't adhere to an interface.
What could solve this would be another magic like __proxy

private function __proxy() {
    return $this->origin;
}

Where every method missing from Decorator would be proxied to an origin (Target) and it would adhere to the interface.
Although, this is just to save on keystrokes, you can still do it.

@Ocramius
Copy link

@nicholasruunu that's already provided by the userland library mentioned above

@Potherca
Copy link

Potherca commented Jun 3, 2017

// Doesn't work becuase type-hints are a waste of time and lead to poorly coded, brittle systems... :)

I'm just gonna grammar nazi this and point out that "becuase" is spelled wrong.

Also, you seem to have misspelled "type-hints" as that is not how you spell "idiots" ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment