在call_user_func或call_user_func_array中调用parent :: function

时间:2015-07-07 10:47:00

标签: php

假设我有以下类方法:

public function handleException(fn, args) {
   try {
       call_user_func_array(fn, args);
   } catch (SomeException $e) {
       //handle
   }
}

function fn1(x, y) {
   return $this->handleException(parent::fn1, [x, y]);
}

function fn2(x, y) {
   return $this->handleException(parent::fn2, [x, y]);
}

此代码不起作用。 我想知道是什么原因(课程不是一等公民?)

1 个答案:

答案 0 :(得分:1)

使用引号

class A
{
    function f1($arg)
    {
        echo $arg;
    }
}

class B extends A
{
    function test()
    {
        call_user_func_array('parent::f1', ['param1']);
    }
}


$b = new B();
$b->test();