Last active
August 29, 2015 13:56
-
-
Save camspiers/9105792 to your computer and use it in GitHub Desktop.
Make method on classes accessible as a Closure via property get
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Camspiers; | |
use ReflectionMethod; | |
/** | |
* Class ClosureAccess | |
* @package Camspiers | |
*/ | |
trait ClosureAccess | |
{ | |
/** | |
* @var array | |
*/ | |
protected $closureMethods = []; | |
/** | |
* @param $name | |
* @param $args | |
* @return mixed | |
*/ | |
public function __call($name, $args) | |
{ | |
if (isset($this->$name) && $this->$name instanceof \Closure) { | |
return call_user_func_array($this->$name, $args); | |
} | |
} | |
/** | |
* @param $name | |
* @throws \InvalidArgumentException | |
* @return callable | |
*/ | |
public function __get($name) | |
{ | |
if (method_exists($this, $name)) { | |
if (empty($this->closureMethods[$name])) { | |
if (!(new ReflectionMethod($this, $name))->isPublic()) { | |
throw new \InvalidArgumentException(sprintf( | |
"Method %s::%s is not public and can't be accessed though ClosureAccess", | |
__CLASS__, | |
$name | |
)); | |
} | |
$this->closureMethods[$name] = function () use ($name) { | |
return call_user_func_array([$this, $name], func_get_args()); | |
}; | |
} | |
return $this->closureMethods[$name]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment