Reference to static method in PHP?

前端 未结 8 2401
伪装坚强ぢ
伪装坚强ぢ 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 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');
        }
    
    }
    

提交回复
热议问题