Skip to content

Instantly share code, notes, and snippets.

@camspiers
Last active August 29, 2015 13:56
Show Gist options
  • Save camspiers/9105792 to your computer and use it in GitHub Desktop.
Save camspiers/9105792 to your computer and use it in GitHub Desktop.
Make method on classes accessible as a Closure via property get
<?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