[英]How to run method before others?
I need to run one private method before some others public methods, so i used __call
method which seems to override default method calling so it looks like this 我需要先运行一个私有方法,然后再运行其他一些公共方法,所以我使用了
__call
方法,它似乎覆盖了默认方法的调用,因此它看起来像这样
function __call($method, $arguments)
{
// echo($method);
if (in_array($method, array('changeStatus', 'lockStatus')))
$this->_checkInputData($arguments);
call_user_func($method, $arguments);
}
But i'm suddenly noticed that __call
method works only on non-defined methods, so is there any way to call custom method before specified? 但是我突然注意到
__call
方法仅适用于未定义的方法,因此有没有办法在指定之前调用自定义方法?
This question is a bit odd. 这个问题有点奇怪。 To call private method before public you can call it in your public method example
要在公开之前调用私有方法,可以在您的公开方法示例中调用它
class Example
{
private function methodA()
{
echo 'I am a method A';
}
public function methodB()
{
$this->methodA();
echo 'I am a method B';
}
}
in that way when you call methodB on object it will call firstly methodA and then body of method B. 这样,当您在对象上调用methodB时,它将首先调用methodA,然后再调用方法B的主体。
Edit 编辑
you wanted to specify what method should be called. 您想指定应调用的方法。
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
}
}
In this way you can specify in 这样,您可以在
$obj = new Example();
$obj->callMethods('a'); // will call firstly MethodA
$obj->callMethods('b'); // will call firstly MethodB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.