Reputation: 48919
Would be possible to specify a default argument value when argument is a PHP closure? Like:
public function getCollection($filter = function($e) { return $e; })
{
// Stuff
}
Am i missing something (maybe a different syntax?) or it's not possible at all? Of course i know i can do:
public function getCollection($filter = null)
{
$filter = is_callable($filter) ? $filter : function($e) { return $e; };
// Stuff
}
(NOTE: I didn't test the above code)
Upvotes: 16
Views: 2856
Reputation: 551
What about the following?
public function getCollection(?Closure $filter = null)
{
$filter ??= fn($e) => $e;
// Stuff
}
Upvotes: 0
Reputation: 227280
Default arguments can only be "scalar arguments", arrays, or NULL.
"scalar values" in PHP are numbers, strings, and booleans.
If you want a function to be a default argument, you're gonna need to use the 2nd way, the 1st is a syntax error.
Upvotes: 18