Passing static methods as arguments in PHP

前端 未结 7 1407
温柔的废话
温柔的废话 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);
    

提交回复
热议问题