PHP魔术方法之call与callStatic方法

WBOY
Release: 2016-06-23 13:09:20
Original
839 people have browsed it

__call方法用于调用类中未定义的方法,__callStatic方法用于调用类中为调用的静态方法,两个函数都接受两个参数,第一个参数为函数名,第二个为传递的参数。与其他函数调用方式不同的是第一个参数的值会自动是调用的名字,第二个参数为数组传递过来的。

<?php    class human{      private function t(){    }    //魔术方法__call  /* $method 获得方法名 $arg 获得方法的参数集合 */  public function __call($method,$arg){      echo '你想调用我不存在的方法',$method,'方法<br/>';      echo '还传了一个参数<br/>';      echo print_r($arg),'<br/>';    }    //魔术方法__callStatic  public static function __callStatic($method,$arg){      echo '你想调用我不存在的',$method,'静态方法<br/>';      echo '还传了一个参数<br/>';      echo print_r($arg),'<br/>';    }    }    $li=new human();    $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'));  */     ?>
Copy after login


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!