Home > Backend Development > PHP Tutorial > php魔术方法call

php魔术方法call

WBOY
Release: 2016-06-23 13:36:54
Original
1102 people have browsed it

        __call是魔术方法中的一个,当程序调用到当前类中未声明或没权限调用的方法时,就会调用__call方法

class test{  public function emptyFunc(){    $getArgs = func_get_args();    $funcName = $getArgs[0];    //$params = array_slice($getArgs, 1);    //var_dump($params);  // this is params                    return $funcName . ' function is not exists';  }  public function __call($m, $params)  {     $arr[] = $m;     $arr = array_merge($arr, $params);     return call_user_func_array(array($this, 'emptyFunc'), $arr);  }  protected function nowToTest(){    return 'this is nowToTest';  }}$testObj = new test();var_dump($testObj->nowToTest('params1','params1'));//var_dump result => string(29) "nowToTest function is not exists"
Copy after login

如上test类中,nowToTest方法是存在的,但修饰这方法的是protected(保护),所以实例出来的对象没权限执行,这时就跑到__call中去了.

_call()有2个参数,第一个$m是当前调用方法的名字,这里是'nowToTest',第二个$params是调用'nowToTest'方法时传入的参数。以数组的形式组合在$params中。

call_user_func_array($method, $params)这个php方法的作用是调用 $method方法,参数为$params,如果方法是在类中的话,就用上面那种数组形式调用就可以了,这里调用的是emptyFunc方法。 func_get_args()的作用是以数组形式获取传入的所有参数。而这些参数在__call中传入的,第一个参数就是方法名。所以最后返回的结果是

"nowToTest function is not exists"
Copy after login
















Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template