Why approach number 1 works
This is a priority artifact of the PHP statement.
$function = self::$function and $function();
equivalently
($function = self::$function) and $function();
which of course is equivalent
$function = self::$function; if ($function) $function();
The last line works fine because the syntax is for variable functions .
Why approach number 2 does not work
Because there is ambiguity that the compiler resolves "in the wrong way."
Consider this:
self::$function();
Does this mean that PHP should call the callback stored in the static $function property, or that it should call the static method whose name is the value of the $function variable?
PHP comes with the latter, but there is no $function variable in the area, leading first to a notification and then to an error ( $function evaluates to null , which is not a string).
source share