"$ this-> array []()"是什么意思?

Joh*_*rgo 2 php arrays function

我无法理解代码,这类似于: - $this->array[$key]($parameter)

为什么会出现($parameter)$this->array[$key]

谢谢

Jef*_*ert 6

作为参考,这里引用的代码片段是以下单行函数:

/**
 * Call a custom driver creator.
 *
 * @param  string  $name
 * @param  array  $config
 * @return mixed
 */
protected function callCustomCreator($name, array $config)
{
    return $this->customCreators[$config['driver']]($this->app, $name, $config);
}
Run Code Online (Sandbox Code Playgroud)

保存在该$this->customCreators[$config['driver']]代码段中表示的位置的值是a function.您通常会调用这样的命名函数:

functionName();
Run Code Online (Sandbox Code Playgroud)

打开/关闭括号告诉PHP 调用/执行该函数而不是仅引用它,这意味着您可以将该函数作为参数传递给单独的函数,如下所示:

anotherFunction($this->customCreators[$config['driver']]);

function anotherFunction($creatorFn) {
    $creatorFn();
}
Run Code Online (Sandbox Code Playgroud)

PHP 在5.3版本中添加了lambda样式函数的支持(PHP使用术语"匿名"),这时您可以说我们开始将函数视为一等公民.


小智 5

在您引用的代码中,数组包含一个使用指定参数调用的函数.它只是一个常规函数调用,但函数(或者更确切地说是对它的引用)存储在数组中.