__call和__callStatic

来源:互联网 发布:medline数据库网址 编辑:程序博客网 时间:2024/05/20 21:42
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />  <?phpclass Demo{ public function __call($method,$args){//遍历参数args$var='';foreach($args as $value){$var.=$value.',';//字符串连接运算}return '方法是'.$method.'('.$var.')'.'不存在';}//当我们调用一个不存在的静态方法时,会自动调用__callStatic()  public static function __callStatic($method,$args){//遍历参数args$var='';foreach($args as $value){$var.=$value.',';//字符串连接运算}return '静态方法是'.$method.'('.$var.')'.'不存在';}}//当访问一个不存在的静态方法时,自动调用类中的魔术方法:__call()  $obj=new Demo;  echo $obj->hello('php','python');  echo '<hr>';  //当访问一个不存在的静态方法时,自动调用类中的魔术方法__callStatic()  echo Demo::hello(1,2,3);  ?>