views:

103

answers:

4

Hey,

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?

Thank you in advance for your answers :)

+6  A: 

$func simply returns its argument, so:

$var = $func(function($str = "asd"){ return $str; });

is the same as:

$var = function($str = "asd"){ return $str; };

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 __invoke magic 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.

php > $func = function($str){ return $str; };
php > $var = function($str = "asd"){ return $str; };
php > var_dump($var);
object(Closure)#2 (1) {
  ["parameter"]=>
  array(1) {
    ["$str"]=>
    string(10) "<optional>"
  }
}
Matthew Flaschen
Thanks, this was very helpful, to understand what i'm doing :)
Nort
+2  A: 

In second example $func just returns the function set via parameters..

Easiest way to show you is this way..

$func = function($str){ return $str(); }; 
$var = $func(function($str = "asd"){ return $str; }); 

var_dump($var); 

Note the () after 'return $str' in $func

Reply to Comment:

$func = function($str, $value){ return $str($value); }; 
$var = $func(function($str = "asd"){ return $str; }, 'efg'); 

var_dump($var); 
Phliplip
Understand? The function is returned not executed. Thus the () after $str wil execute it.
Phliplip
yep, i understood, and how can add a value to the `$str` variable in the unnamed parameter, function?
Nort
Se my reply to your comment in the answer
Phliplip
Please rephrase question :)
Phliplip
A: 
 $func = function($str){ return $str; };

Generates a Closure object with a __invoke() function.

This object is passed as a parameter in the second example. And because the anonimimus function returns the input the var_dump() display the function object instead of the result.

Too bad chaining isn't supported (like in javascript)

 $func = function($str){ return $str; };
 $var = $func(function($str = "asd"){ return $str; }('test'));
 var_dump($var);

Could result in "test" but generates a PHP Parse error: syntax error, unexpected '('

Bob Fanger
+2  A: 
$func = function($str){ return $str; };
$var = $func(function($str = "asd"){ return $str; });

var_dump($var);

First you define a function $func which simply returns the first parameter. So $var becomes function($str = "asd"){ return $str; }.

I can recommend the IBM article about closures and lambda functions http://www.ibm.com/developerworks/opensource/library/os-php-5.3new2/index.html.

svens