<?php
class aaa{
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/>';
}
}
$a=new aaa();
$a->xx(1,2,3);
/*
调用一个未定义的方法
Fatal error: Call to undefined method aaa::xx() in D:\wamp\www\php\aaa.php on line 8
*/
$li->t('a','b');
/*
__call是调用不可见(不存在或无权限)的方法时,自动调用
$a->xx(1,2,3);-----没有xx()方法----> __call('xx',array(1,2,3))运行
*/
aaa::yy('a','b','c');
/*
__callStatic 是调用不可见的静态方法时,自动调用.
aaa::yy('a','b','c')----没有yy方法---> aaa::__callStatic('yy',array('a','b','c'));
*/
?> 版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了PHP魔术方法之__call与__callStatic方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号