Passing static methods as arguments in PHP

前端 未结 7 1403
温柔的废话
温柔的废话 2021-02-02 07:34

In PHP is it possible to do something like this:

myFunction( MyClass::staticMethod );

so that \'myFunction\' will have a reference to the stati

相关标签:
7条回答
  • 2021-02-02 08:05

    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);
    
    0 讨论(0)
  • 2021-02-02 08:13

    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.

    0 讨论(0)
  • 2021-02-02 08:13

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

    0 讨论(0)
  • 2021-02-02 08:15

    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.

    0 讨论(0)
  • 2021-02-02 08:17

    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') );
    
    0 讨论(0)
  • 2021-02-02 08:20

    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.

    0 讨论(0)
提交回复
热议问题