I’m not sure that silly question, but I ask:
So, if there is an anonymous function I can give it as another anonymous functions parameter, if it has been already stored a variable.
But, whats in that case, if I have stored only one function in a variable, and add the second directly as a parameter into it? Can I add parameters to the non-stored function?
Fist example (thats what i understand 🙂 ):
$func = function($str){ return $str; };
$func2 = function($str){ return $str; };
$var = $func($func2('asd'));
var_dump($var);
// prints out string(3) "asd"
That makes sense for me, but what is with the following one?
$func = function($str){ return $str; };
$var = $func(function($str = "asd"){ return $str; });
var_dump($var);
/** This prints out:
object(Closure)#1 (1) {
["parameter"]=>
array(1) {
["$str"]=>
string(10) ""
}
}
But why?
*/
And at the end, can someone recommend me a book or an article, from what i can learn this lambda coding feature of php?
$funcsimply returns its argument, so:is the same as:
The fact that anonymous functions are instances of Closure is an implementation detail the docs warn against relying on. There’s nothing special about Closure. It can be implemented in regular PHP thanks to the
__invokemagic method.On my PHP 5.3.2 installation, Closure has a different representation. The parameter is marked with an
"<optional>"string. Perhaps this is a formatting issue.