Php 如何获取闭包对象';s参数随use语句一起传递
这段代码来自slim的Route.phpPhp 如何获取闭包对象';s参数随use语句一起传递,php,slim,Php,Slim,这段代码来自slim的Route.php public function getCallable() { return $this->callable; } /** * Set route callable * @param mixed $callable * @throws \InvalidArgumentException If argument is not callable */ public function setCallable($callable) {
public function getCallable()
{
return $this->callable;
}
/**
* Set route callable
* @param mixed $callable
* @throws \InvalidArgumentException If argument is not callable
*/
public function setCallable($callable)
{
$matches = array();
if (is_string($callable) && preg_match('!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!', $callable, $matches)) {
$class = $matches[1];
$method = $matches[2];
$callable = function() use ($class, $method) {
static $obj = null;
if ($obj === null) {
$obj = new $class;
}
return call_user_func_array(array($obj, $method), func_get_args());
};
}
if (!is_callable($callable)) {
throw new \InvalidArgumentException('Route callable must be callable');
}
$this->callable = $callable;
}
我试图做的是在钩子中获取路由处理程序类名和方法
这是我的钩子代码
$app = \Slim\Slim::getInstance();
$app->hook('slim.before.dispatch', function () use ($app)
{
$route = $app->router()->getCurrentRoute();
$callable = $route->getCallable();
$callable = $callable->bindTo(null);
$objReflector = new \ReflectionObject($callable);
});
执行print\r($callable)
Closure Object
(
[static] => Array
(
[class] => \UserGroup\Controllers\AdminController
[method] => index
[obj] =>
)
)
ReflectionObject Object
(
[name] => Closure
)
和print\r($objReflector)
Closure Object
(
[static] => Array
(
[class] => \UserGroup\Controllers\AdminController
[method] => index
[obj] =>
)
)
ReflectionObject Object
(
[name] => Closure
)
我想获取值[class]=>\UserGroup\Controllers\AdminController
和[method]=>索引
我希望任何人都能帮忙
$objReflector->name->static['class']
?$objReflector->name
是一个string@RajneeshKumarOjha,在我看来,您需要的是ReflectionMethod
,而不是ReflectionObject
。对象将与整个控制器相关,而不是专门与index
方法相关。顺便说一句,你到底想实现什么?$objReflector->name->static['class']
?$objReflector->name
是一个string@RajneeshKumarOjha,在我看来,您需要的是ReflectionMethod
,而不是ReflectionObject
。对象将与整个控制器相关,而不是专门与index
方法相关。顺便说一句,你到底想达到什么目的?