Passing static methods as arguments in PHP

class, php, static

Solution

The 'php way' to do this, is to use the exact same syntax used by is_callable and call_user_func.

This means that your method is 'neutral' to being

- A standard function name

- A static class method

- An instance method

- A closure

In the case of static methods, this means you should pass it as:

myFunction( [ 'MyClass', 'staticMethod'] );

or if you are not running PHP 5.4 yet:

myFunction( array( 'MyClass', 'staticMethod') );

Problem

In PHP is it possible to do something like this: ``` myFunction( MyClass::staticMethod ); ``` so that 'myFunction' will have a reference to the static method and be able to call it. When I try it, I get an error of "Undefined class constant" (PHP 5.3) so I guess it isn't directly possible, but is there a way to do something similar? The closest I've managed so far is pass the "function" as a string and use call_user_func().

Original source

Related problems