PHP魔术方法之__call与__callStatic方法
2016-10-30 16:29
267 查看
<?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->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');
注意:这个位置调用t这个方法也直接进入到__call()去了,因为t是私有方法,在类的外部调用不到
/*
__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'));
*/
?>
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->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');
注意:这个位置调用t这个方法也直接进入到__call()去了,因为t是私有方法,在类的外部调用不到
/*
__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'));
*/
?>
相关文章推荐
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toStr
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP魔术方法之__call与__callStatic方法
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload
- PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload
- php魔术方法__call和__callStatic
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- php -- 魔术方法 之 调用方法:__call()、__callStatic()
- PHP魔术方法之__call与__callStatic方法
- PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
- PHP中的魔术方法总结 :__construct,__destruct ,__call,__callStatic,__get,__set,__isset,__unset,__sleep,__wakeup, __toString.......
- 回顾php魔术方法__call(),__callStatic()
- php的魔术方法__get(),__set(),__call(),__callStatic()以及static延迟静态绑定
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- php的魔术方法__get(),__set(),__call(),__callStatic()以及static延迟静态绑定
- PHP中的魔术方法之__call()和__callStatic()方法的使用
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP 魔术方法之 __call 与 __callStatic