Is there a way to do this in php?
//in a class
public static function myFunc($x = function($arg) { return 42+$arg; }) {
return $x(8); //return 50 if default func is passed in
}
PHP Default function arguments can only be of scalar or array types:
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
From: PHP Manual / Function Arguments / Default argument values
How about:
public static function myFunc($x = null) {
if (null === $x) {
$x = function($arg) { return 42 + $arg; };
}
return $x(8); //return 50 if default func is passed in
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With