我無法理解的代碼,這是一樣的東西: - $this->array[$key]($parameter)
爲什麼會出現($parameter)
$this->array[$key]
後?
感謝
我無法理解的代碼,這是一樣的東西: - $this->array[$key]($parameter)
爲什麼會出現($parameter)
$this->array[$key]
後?
感謝
作爲參考,將代碼段被稱爲這裏是以下單行功能:
/**
* 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);
}
被保持在由$this->customCreators[$config['driver']]
在於代碼段所表示的位置的值是一個function
。通常你調用一個名爲函數是這樣的:
functionName();
打開/關閉括號告訴PHP 呼叫/執行該功能而不僅僅是參考它,這意味着你可以四處傳遞函數到一個單獨的功能作爲參數這樣的:
anotherFunction($this->customCreators[$config['driver']]);
function anotherFunction($creatorFn) {
$creatorFn();
}
PHP added support爲lambda-style功能(PHP使用的術語是「匿名」)5.3版本,這是當你可以說,我們啓動處理函數爲first-class citizens。
在你引用的代碼,該數組包含一個都會調用指定的參數功能。這只是一個常規的函數調用,但函數(或者說對它的引用)存儲在數組中。
從技術上講,OP所提供的示例實際上並不起作用,因爲'[]'僅用於將元素推送到數組上。 – Dencker
是的,他在問題中發佈的代碼行不起作用。我指的是他發佈的GitHub鏈接。 –
http://php.net/manual/en/functions.variable-functions.php – jolmos