魔术方法__call

//先讲解__call方法的基础使用

//
__call魔术方法,当调用一个类的方法,此方法不存在 //就会执行__call方法 class Computer { public function _run() { echo '运行'; } //采用__call()方法屏蔽调用 //__call()里面有两个参数 //第一个参数为调用的那个方法名 //第二个参数为方法中传入的值 public function __call($_methodName,$_argList) { echo $_methodName.'()not exist'; //print_r($_argList); } } $computer = new Computer(); //如果quiet()方法不存在,执行__call()方法 $computer->quiet(); //------------------ class MethodTest { public function __call($name, $arguments) { // 注意: $name 的值区分大小写 echo "Calling object method '$name' " . implode(', ', $arguments). "\n"; } /** PHP 5.3.0之后版本 */ public static function __callStatic($name, $arguments) { // 注意: $name 的值区分大小写 echo "Calling static method '$name' " . implode(', ', $arguments). "\n"; } } $obj = new MethodTest; $obj->runTest('in object context'); MethodTest::runTest('in static context'); // PHP 5.3.0之后版本 //以上例程会输出: //Calling object method 'runTest' in object context //Calling static method 'runTest' in static context
原文地址:https://www.cnblogs.com/loveyouyou616/p/2806632.html