0

我需要在其他一些公共方法之前运行一个私有方法,所以我使用__call了似乎覆盖默认方法调用的方法,所以它看起来像这样

function __call($method, $arguments)
{
    // echo($method);

    if (in_array($method, array('changeStatus', 'lockStatus'))) 
        $this->_checkInputData($arguments);

    call_user_func($method, $arguments);
}

但是我突然注意到该__call方法仅适用于未定义的方法,那么有没有办法在指定之前调用自定义方法?

4

1 回答 1

0

这个问题有点奇怪。要在公共方法之前调用私有方法,您可以在公共方法示例中调用它

class Example
{
   private function methodA()
   {
     echo 'I am a method A';
   }

   public function methodB()
   {
     $this->methodA();
     echo 'I am a method B';
   }
}

这样,当您在对象上调用 methodB 时,它将首先调用 methodA,然后调用方法 B 的主体。

编辑

你想指定应该调用什么方法。

class Example
{
   private function methodA()
   {
     echo 'I am a method A';
   }

   private function methodB()
   {
     echo 'I am a method B';
   }

   public function callMethods($method)
   {
      if($method =='a') $this->methodA();
      else if($method =='b') $this->methodA();
      //rest of function  
   }
}

通过这种方式,您可以指定

$obj = new Example();
$obj->callMethods('a'); // will call firstly MethodA
$obj->callMethods('b'); // will call firstly MethodB
于 2013-06-07T11:44:58.910 回答