Passing static methods as arguments in PHP

2019-03-10 16:50发布

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().

标签: php class static
6条回答
对你真心纯属浪费
2楼-- · 2019-03-10 17:37

Let my try to give a thorough example...

You would make a call like this:

myFunction('Namespace\\MyClass', 'staticMethod');

or like this (if you have arguments you want to pass):

myFunction('Namespace\\MyClass', 'staticMethod', array($arg1, $arg2, $arg3));

And your function to receive this call:

public static function myFunction($class, $method, $args = array())
{
    if (is_callable($class, $method)) {
        return call_user_func_array(array($class, $method), $args);
    }
    else {
        throw new Exception('Undefined method - ' . $class . '::' . $method);
    }
}

Similiar technique is commonly used in the Decorator Pattern in php.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-10 17:43

to the question:

In PHP is it possible to do something like this:

myFunction( MyClass::staticMethod );

the answer is yes. you can have the staticMethod() return anonymous function instead. i.e.

private static function staticMethod()
{
    return function($anyParameters)
    {
        //do something here what staticMethod() was supposed to do
        // ...
        // ...
        //return something what staticMethod() was supposed to return;
    };
}

you can then write

myFunction(MyClass::staticMethod());

but note that () is needed to invoke staticMethod(). This is because it now returns the anonymous function that wraps the job of what you initially wanted your staticMethod() to do.

This works perfectly fine when staticMethod() is only being passed in as parameter into another function. If you wish to call staticMethod() that directly does the processing, you have to then write

MyClass::staticMethod()($doPassInParameters);

Note that this may require one extra redundant step to retrieve the function pointer, as compared to when it can work without wrapping it in anonymous function. I only use it to pass as parameter so am not sure of the performance penalty of the extra step. Perhaps negligible ...

查看更多
Rolldiameter
4楼-- · 2019-03-10 17:47

Since you've already mentioned that call_user_func() has been used and you're not interested in a solution with that or ones that are passing the static function as a string, here is an alternative: using anonymous functions as a wrapper to the static function.

function myFunction( $method ) {
    $method();
}

myFunction( function() { return MyClass::staticMethod(); } );

I wouldn't recommend doing this, as I think the call_user_func() method is more concise.

查看更多
beautiful°
5楼-- · 2019-03-10 17:53

this will show 6 for the the first call and 9 for the second call in output.

$staticmethod1 = function ($max)
{
    return $max*2;
};

$staticmethod2 = function ($max)
{
    return $max*$max;
};

function myfunction($x){
    echo $x(3);
}

myfunction($staticmethod1);
myfunction($staticmethod2);
查看更多
Melony?
6楼-- · 2019-03-10 17:54

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') );
查看更多
放荡不羁爱自由
7楼-- · 2019-03-10 17:55

If you want to avoid strings, you can use this syntax:

myFunction( function(){ return MyClass::staticMethod(); } );

It is a little verbose, but it has the advantage that it can be statically analysed. In other words, an IDE can easily point out an error in the name of the static function.

查看更多
登录 后发表回答