PHP魔术方法之__call与__callStatic方法
来源:互联网 发布:linux fsck修复危险性 编辑:程序博客网 时间:2024/06/04 19:20
- <?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');
- /*
- __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
- //获得每个城市天气预报
- class Action{
- public function tj(){
- echo 'tj天气预报<br/>';
- }
- /*
- $m 方法名
- $p 方法参数集合
- */
- public function __call($m,$p){
- echo $m,'天气预报<br/>';
- }
- }
- $c=new Action();
- $c->tj();
- //获得城市
- $city=$_GET['method'];
- if(isset($city)){
- //获得城市的方法,由魔术方法__call处理
- $c->$city();
- }
- /*
- 网址:http://localhost/php/60.php?method=beijing
- 结果:
- tj天气预报
- beijing天气预报
- */
- ?>
0 0
- PHP 魔术方法之 __call 与 __callStatic
- PHP魔术方法之__call与__callStatic方法
- PHP魔术方法之__call与__callStatic方法
- PHP魔术方法之__call与__callStatic方法
- PHP魔术方法之__call与__callStatic方法
- PHP魔术方法之__call与__callStatic方法
- PHP魔术方法之__call与__callStatic方法
- 魔术方法(__call/__callstatic)
- php魔术方法__call和__callStatic
- 魔术方法__call 和__callStatic
- php的__call 与__callStatic 方法
- PHP 魔法方法之__call和__callStatic
- php 魔术方法 __call
- php 魔术方法 __call
- PHP魔术方法之__call重载方法
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __uns
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep
- unix网络编程-第3,4章套接字编程
- 为了女儿.500租了一个老公
- linux点滴0
- Java 4种引用 strongReference, softReference, weakReference, phantomReference
- 好记性不如写博客
- PHP魔术方法之__call与__callStatic方法
- python写文件出错
- 如何解决卸载McAfee时出现“处于托管模式时无法删除”问题
- Android Studio如何import module(针对非gradle)
- 无所畏惧,学无止境
- 编辑框控件的两种成员变量(CEdit控件类型与CString 值类型之间的关联问题)
- 爱情if else
- WebView---Android与js交互实例
- SVN Working copying 'xxxxx/xxxx/xxxx' locked