Skip to content

Instantly share code, notes, and snippets.

@shov
Last active December 7, 2017 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shov/85919ad65a742a1e45f62b553e15c046 to your computer and use it in GitHub Desktop.
Save shov/85919ad65a742a1e45f62b553e15c046 to your computer and use it in GitHub Desktop.
PHP Implementation of the decorator as the Trait
<?php
/**
* Trait Decorator
*
* Required on host
* @method getDecoratedObject()
*/
trait Decorator
{
/**
* @param $method
* @param $args
* @return mixed
* @throws \Exception
*/
public function __call($method, $args)
{
if (is_callable([$this->getDecoratedObject(), $method])) {
return call_user_func_array([$this->getDecoratedObject(), $method], $args);
}
throw new \Exception(
sprintf("Call undefined method: %s::%s", get_class($this->getDecoratedObject()), $method));
}
/**
* @param $property
* @return null
*/
public function __get($property)
{
$publicVars = get_object_vars($this->getDecoratedObject());
if (array_key_exists($property, $publicVars)) {
return $this->getDecoratedObject()->$property;
}
return null;
}
/**
* @param $property
* @param $value
* @return $this
*/
public function __set($property, $value)
{
$this->getDecoratedObject()->$property = $value;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment