Reputation: 13
I am learning more about closures, and I want to re-create something similar to Laravel's route function.
SO i have the following code:
<?php
Class Foo{
public static function show($second, $third){
return "First " . $second . $third;
}
}
echo $my_var = Foo::show("Second ", function(){
return "Third ";
});
but if I run it, I get "Catchable fatal error: Object of class Closure could not be converted to string " error.
If I remove the variable $third from the Foo::show function, theres no errors given, but of course i dont see the third variable.
I was expecting result : First Second Third;
What gives ? I am just learning.. :)
Upvotes: 0
Views: 4000
Reputation: 54841
As $third
is a function, to get it's returned value - you must call it. Function call is usually done with ()
, so the change is:
return "First " . $second . $third();
Here, function passed as $third
argument is executed, string Third
returned and concatenated with previous string.
Upvotes: 1