降级类php 5.6到5.5

M B*_*ers 6 php php-5.6 php-5.5

我创建了一个用于堆栈对象层和闭包的类,但我的服务器还没有在php 5.6上运行.我想知道我怎么可以转换的...$parameters,因为我不能用替代一切修复它call_user_func_array()那么buildCoreClosure()方法将抛出错误,例如,由于关闭心不是一个数组...

class Stack
{
    /**
     * Method to call on the decoracted class.
     *
     * @var string
     */

    protected $method;

    /**
     * Container.
     */

    protected $container;

    /**
     * Middleware layers.
     *
     * @var array
     */

    protected $layers = [];

    public function __construct(Container $container = null, $method = null)
    {
        $this->container = $container ?: new Container;
        $this->method = $method ?: 'handle';
    }

    public function addLayer($class, $inner = true)
    {
        return $inner ? array_unshift($this->layers, $class) : array_push($this->layers, $class);
    }

    public function addInnerLayer($class)
    {
        return $this->addLayer($class);
    }

    public function addOuterLayer($class)
    {
        return $this->addLayer($class, false);
    }

    protected function buildCoreClosure($object)
    {
        return function(...$arguments) use ($object)
        {
            $callable = $object instanceof Closure ? $object : [$object, $this->method];

            return $callable(...$arguments);
        };
    }

    protected function buildLayerClosure($layer, Closure $next)
    {
        return function(...$arguments) use ($layer, $next)
        {
            return $layer->execute(...array_merge($arguments, [$next]));
        };
    }


    public function peel($object, array $parameters = [])
    {
        $next = $this->buildCoreClosure($object);

        foreach($this->layers as $layer)
        {
            $layer = $this->container->get($layer);

            $next = $this->buildLayerClosure($layer, $next);
        }

        return $next(...$parameters);
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 4

通过删除

...$arguments
Run Code Online (Sandbox Code Playgroud)

在每个函数的参数列表中并将其替换为以下内容(在函数的早期部分)

$arguments = func_get_args();
Run Code Online (Sandbox Code Playgroud)

您可以获得相同的参数值。即使未在函数的参数列表中定义,参数仍将传递给 func_get_args() 。