__call 当要调用的方法不存在或权限不足时,会自动调用__call 方法。
__callStatic 当调用的静态方法不存在或权限不足时,会自动调用__callStatic方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php /* class Human { public function hello() { echo 'hello<br />'; } } $lisi = new Human(); $lisi->hello(); $lisi->say(); //没有此方法,报错如下 // 报错:Fatal error: Call to undefined method Human::say() */ class Human { public function hello() { echo 'hello<br />'; } private function t() { } public static function __callStatic($method,$argu) { echo '你想调用一个不存在或不权调用的静态方法',$method,'<br />'; echo '你调用时还传了参数<br />'; print_r($argu); } public function __call($method,$argu) { echo '你想调用一个我不存在或不权调用的方法',$method,'<br />'; echo '你调用时还传了参数<br />'; print_r($argu); } } $lisi = new Human(); $lisi->hello(); $lisi->say(1,2,3); $lisi->t('a','b','c'); /* __call是调用不可见(不存在或无权限)的方法时,自动调用 $lisi->say(1,2,3);-----没有say()方法----> __call('say',array(1,2,3))运行 */ Human::cry('痛哭','号哭','鬼哭'); /* __callStatic 是调用不可见的静态方法时,自动调用. Human::cry('a','b','c')----没有cry方法---> Human::__callStatic('cry',array('a','b','c')); */ /* 这个__callStatic 为什么和其他系统函数颜色不太一样呢? 答: 这个方法 是PHP5.3里才添加的,比较新. 可能老版本的ediptlus的语法文件里,没有他 */ |
__call 在thinkPHP中的应用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php header("Content-type: text/html; charset=utf-8"); class Action { public function bj() { echo 'bj天气预报'; } public function __call($m,$args) { echo $m,'天气预报'; } } $action = new Action(); $method = $_GET['method']; if($method) { $action->$method(); } //http://localhost/1113/04.php?method=沂源 //沂源天气预报 |

我的微信
把最实用的经验,分享给最需要的读者,希望每一位来访的朋友都能有所收获!