In PHP, you can call functions by calling their names inside a variable.
function myfunc(){ echo 'works'; }
$func = 'myfunc';
$func();
But you cannot do this with constants.
define('func', 'myfunc');
func();
There are workarounds like these:
$f = func;
$f();
call_user_func(func);
function call($f){ $f(); }
call(func);
The PHP documentation says: callable
The PHP function is passed by name as a string. You can use any built-in or user-defined function, except for language constructs.
There seems to be nothing about constant values that cannot be called.
I also tried to verify this and, of course,
var_dump(is_callable(func));
prints bool(true).
Now, is there an explanation why this is so? As far as I can see, all workarounds depend on assigning a constant value to a variable, why can't a constant be called?
, , , . , PHP .