Reference to static method in PHP?

前端 未结 8 2360
伪装坚强ぢ
伪装坚强ぢ 2021-02-18 18:20

In PHP, I am able to use a normal function as a variable without problem, but I haven\'t figured out how to use a static method. Am I just missing the right syntax, or is this

相关标签:
8条回答
  • 2021-02-18 18:55

    You could use the full name of static method, including the namespace.

    <?php
        function foo($method)
        {
            return $method('argument');
        }
    
        foo('YourClass::staticMethod');
        foo('Namespace\YourClass::staticMethod');
    

    The name array array('YourClass', 'staticMethod') is equal to it. But I think the string may be more clear for reading.

    0 讨论(0)
  • 2021-02-18 19:04

    "A member or method declared with static can not be accessed with a variable that is an instance of the object and cannot be re-defined in an extending class"

    (http://theserverpages.com/php/manual/en/language.oop5.static.php)

    0 讨论(0)
  • 2021-02-18 19:07

    In php 5.2, you can use a variable as the method name in a static call, but to use a variable as the class name, you'll have to use callbacks as described by BaileyP.

    However, from php 5.3, you can use a variable as the class name in a static call. So:

    class Bar
    {
        public static function foo2($a,$b) { return $a/$b; }
    
        public function UseReferences()
        {
            $method = 'foo2';
            print Bar::$method(6,2); // works in php 5.2.6
    
            $class = 'Bar';
            print $class::$method(6,2); // works in php 5.3
         }
    }
    
    $b = new Bar;
    $b->UseReferences();
    ?>
    
    0 讨论(0)
  • 2021-02-18 19:07

    In addition to what was said you can also use PHP's reflection capabilities:

    class Bar {
    
        public static function foo($foo, $bar) {
            return $foo . ' ' . $bar;
        }
    
    
        public function useReferences () {
            $method = new ReflectionMethod($this, 'foo');
            // Note NULL as the first argument for a static call
            $result = $method->invoke(NULL, '123', 'xyz');
        }
    
    }
    
    0 讨论(0)
  • 2021-02-18 19:11

    PHP handles callbacks as strings, not function pointers. The reason your first test works is because the PHP interpreter assumes foo1 as a string. If you have E_NOTICE level error enabled, you should see proof of that.

    "Use of undefined constant foo1 - assumed 'foo1'"

    You can't call static methods this way, unfortunately. The scope (class) is relevant so you need to use call_user_func instead.

    <?php
    
    function foo1($a,$b) { return $a/$b; }
    
    class Bar
    {
        public static function foo2($a,$b) { return $a/$b; }
    
        public function UseReferences()
        {
            $fn = 'foo1';
            echo $fn(6,3);
    
            $fn = array( 'self', 'foo2' );
            print call_user_func( $fn, 6, 2 );
         }
    }
    
    $b = new Bar;
    $b->UseReferences();
    
    0 讨论(0)
  • 2021-02-18 19:15

    In PHP 5.3.0, you could also do the following:

    <?php
    
    class Foo {
        static function Bar($a, $b) {
            if ($a == $b)
                return 0;
    
            return ($a < $b) ? -1 : 1;
        }
        function RBar($a, $b) {
            if ($a == $b)
                return 0;
    
            return ($a < $b) ? 1 : -1;
        }
    }
    
    $vals = array(3,2,6,4,1);
    $cmpFunc = array('Foo', 'Bar');
    usort($vals, $cmpFunc);
    
    // This would also work:
    $fooInstance = new Foo();
    $cmpFunc = array('fooInstance', 'RBar');
    // Or
    // $cmpFunc = array('fooInstance', 'Bar');
    usort($vals, $cmpFunc);
    
    ?>
    
    0 讨论(0)
提交回复
热议问题