Introduced in PHP 5.5, it is possible to use the MyClass::class
syntax to reference a class instead of passing it as 'MyClass'
, which is not easily refactorable as it's just a string.
I would very much like to do the same thing with the function name, when passing a callback/callable to call_user_func()
or usort()
.
In the example
usort($files, [FileFunctions::class, 'dircmp']);
only FileFunctions
can easily be refactored.
How can I reference the dircmp
function in a similar way?
Using
usort($a, FileFunctions::dircmp);
leads to a Undefined class constant 'dircmp'
fatal error, as the parameter after the double colons is interpreted as a constant (of course).