Skip to content

Instantly share code, notes, and snippets.

Created December 14, 2012 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4288929 to your computer and use it in GitHub Desktop.
Save anonymous/4288929 to your computer and use it in GitHub Desktop.
PHP decorator base class
<?php
class BaseDecorator {
private $component;
private $methods = array();
private $properties = array();
public function __construct($component) {
$this->component = $component;
}
public function __call($name, $args) {
if (!isset($this->methods[$name])) {
$this->methods[$name] = method_exists($this->component, $name);
}
if ($this->methods[$name]) {
return call_user_func_array(array($this->component, $name), $args);
}
}
public function __get($name) {
if (!isset($this->properties[$name])) {
$this->properties[$name] = property_exists($this->component, $name);
}
if ($this->properties[$name]) {
return $this->component->$name;
}
}
public function __set($name, $value) {
if (!isset($this->properties[$name])) {
$this->properties[$name] = property_exists($this->component, $name);
}
if ($this->properties[$name]) {
$this->component->$name = $value;
} else {
$this->$name = $value;
}
}
public function __isset($name) {
if (!isset($this->properties[$name])) {
$this->properties[$name] = property_exists($this->component, $name);
}
if ($this->properties[$name]) {
return isset($this->component->$name);
}
}
public function __unset($name) {
if (!isset($this->properties[$name])) {
$this->properties[$name] = property_exists($this->component, $name);
}
if ($this->properties[$name]) {
unset($this->component->$name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment