PHP: passing a function with parameters as parameter

Posted by Oden on Stack Overflow See other posts from Stack Overflow or by Oden
Published on 2010-06-05T10:57:57Z Indexed on 2010/06/05 11:02 UTC
Read the original article Hit count: 280

Filed under:
|
|

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 :)

© Stack Overflow or respective owner

Related posts about php

Related posts about php5