如何使用use语句传递Closure对象的参数

时间:2015-09-18 16:13:02

标签: 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)
{
    $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] => 
    )

)

print_r($objReflector)给出了

ReflectionObject Object
(
 [name] => Closure
)

我希望获得值[class] => \UserGroup\Controllers\AdminController[method] => index

我希望任何人都可以提供帮助

0 个答案:

没有答案