PHP魔术方法之__call与__callStatic方法

';
    echo '还传了一个参数
'; echo print_r($arg),'
'; } //魔术方法__callStatic public static function __callStatic($method,$arg){ echo '你想调用我不存在的',$method,'静态方法
'; echo '还传了一个参数
'; echo print_r($arg),'
'; } } $li=new human(); $li->say(1,2,3); /* 调用一个未定义的方法 Fatal error: Call to undefined method human::say() in D:\wamp\www\php\59.php on line 8 */ $li->t('a','b'); /* __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')); */ ?>



天气预报小实例

';
 }

/*
$m 方法名
$p 方法参数集合
*/
public function __call($m,$p){

  echo $m,'天气预报
'; } } $c=new Action(); $c->tj(); //获得城市 $city=$_GET['method']; if(isset($city)){ //获得城市的方法,由魔术方法__call处理 $c->$city(); } /* 网址:http://localhost/php/60.php?method=beijing 结果: tj天气预报 beijing天气预报 */ ?>


 

你可能感兴趣的:(PHP)