Skip to content

Instantly share code, notes, and snippets.

@mcuyar
Created May 6, 2015 13:59
Show Gist options
  • Save mcuyar/9bc14cca006fbaf47eae to your computer and use it in GitHub Desktop.
Save mcuyar/9bc14cca006fbaf47eae to your computer and use it in GitHub Desktop.
Class Override
<?php
class PublicServant
{
protected $psClass;
protected $ClassName;
private $methods = [];
public function __construct($class)
{
$this->psClass = $class;
$this->psClassName = get_class($class);
}
public function addMethod($name, \Closure $closure)
{
$this->methods[$name] = $this->bind($closure);
}
public function __get($property)
{
return $this->bind(
function($property)
{
return $this->{$property};
}
);
}
public function __set($property, $value)
{
return $this->bind(
function($property)
{
$this->{$property} = $value;
}
);
}
public function __call($method, array $args)
{
if (isset($this->methods[$method])) {
return call_user_func_array($this->methods[$method], $args);
}
return $this->bind(
function($method, $args)
{
return call_user_func_array([$this, $method], $args);
}
);
}
private function bind(\Closure $closure)
{
if (!is_callable($closure)) {
throw new \InvalidArgumentException('Argument 2 must be a closure');
}
return \Closure::bind($closure, $this->psClass, "\\$this->ClassName");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment