I am not able to understand the code, that is something like :-
$this->array[$key]($parameter)
Why is there ($parameter)
after $this->array[$key]
?
Thanks
I am not able to understand the code, that is something like :-
$this->array[$key]($parameter)
Why is there ($parameter)
after $this->array[$key]
?
Thanks
For reference, the code snippet being referred to here is the following one-line function:
/**
* 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);
}
The value being held at the location denoted by $this->customCreators[$config['driver']]
in that code snippet is a function
. You normally call a named function like this:
functionName();
The open/close parentheses tells PHP to call/execute that function rather than just reference it, meaning you can pass that function around to a separate function as a parameter like this:
anotherFunction($this->customCreators[$config['driver']]);
function anotherFunction($creatorFn) {
$creatorFn();
}
PHP added support for lambda-style functions (PHP uses the term 'anonymous') in version 5.3, which is when you can say we started treating functions as first-class citizens.
In the code you referenced, the array contains a function which gets called with the specified parameters. It's just a regular function call, but the function (or rather a reference to it) is stored in the array.