php中__call与__callstatic()使用

php 5.3 后新增了 __call__callStatic 魔法方法。

__call 当要调用的方法不存在或权限不足时,会自动调用__call 方法。

__callStatic 当调用的静态方法不存在或权限不足时,会自动调用__callStatic方法。


__call($funcname, $arguments)

__callStatic($funcname, $arguments)


参数说明:

$funcname String 调用的方法名称。

$arguments Array 调用方法时所带的参数。

__call:


1
<?php 2 class Pc { 3 function __call($name,$arguments) { 4 print("调用的未定义的函数名: $name,"); 5 if(is_int($arguments[0])){ 6 echo '传入的第一个参数为整形数:'.$arguments[0].'<br />'; 7 }else if(is_string($arguments[0])){ 8 echo '传入的第一个参数为字符串:'.$arguments[0].'<br />'; 9 } 10 } 11 } 12 $pc = new Pc(); 13 $pc->fun_pc1(2); 14 $pc->fun_pc2('字符abc'); 15 ?>
/*
调用的未定义的函数名: fun_pc1,传入的第一个参数为整形数:2
调用的未定义的函数名: fun_pc2,传入的第一个参数为字符串:字符abc
*/
 1 <?php
 2 class Pc{
 3     function __call($name,$arguments) {
 4         echo '你调用的函数'.$name.'(参数:';
 5         print_r($arguments);
 6         echo '不存在<br />';
 7     }
 8 }
 9 $pc = new Pc();
10 $pc->fun_pc1(1,2,3);
11 $pc->fun_pc2('a1','a2');
12 ?>
/*
你调用的函数fun_pc1(参数:Array ( [0] => 1 [1] => 2 [2] => 3 ) 不存在
你调用的函数fun_pc2(参数:Array ( [0] => a1 [1] => a2 ) 不存在
*/

 __callstatic:

 1     <?php  
 2       
 3     class Member{  
 4       
 5         protected static $memberdata = array();  
 6       
 7         public static function __callStatic($func, $arguments){  
 8       
 9             list($type, $name) = explode('_', $func);  
10               
11             if(!in_array($type, array('set','get')) || $name==''){  
12                 return '';  
13             }  
14       
15             $feature = get_called_class();  
16       
17             switch($type){  
18                 case 'set':  
19                     self::$memberdata[$feature][$name] = $arguments[0];  
20                     break;  
21       
22                 case 'get':  
23                     return isset(self::$memberdata[$feature][$name])? self::$memberdata[$feature][$name] : '';  
24                     break;  
25       
26                 default:  
27             }  
28           
29         }  
30       
31     }  
32       
33       
34     class User extends Member{  
35       
36         public static function show(){  
37       
38             $feature = get_called_class();  
39       
40             if(self::$memberdata[$feature]){  
41                 foreach(self::$memberdata[$feature] as $key=>$member){  
42                     echo $key.':'.$member.'<br>';  
43                 }  
44             }  
45         }  
46       
47     }  
48       
49       
50     class Profession extends Member{  
51       
52         public static function show(){  
53       
54             $feature = get_called_class();  
55       
56             if(self::$memberdata[$feature]){  
57                 foreach(self::$memberdata[$feature] as $key=>$member){  
58                     echo $key.':'.$member.'<br>';  
59                 }  
60             }  
61         }  
62       
63     }  
64       
65     User::set_name('fdipzone');  
66     User::set_age(29);  
67     User::show();  
68       
69     Profession::set_profession('IT SERVICE');  
70     Profession::set_price(2500);  
71     Profession::show();  
72       
73     ?>  

 

 

 

 

posted @ 2013-09-06 10:23  终于还  阅读(2177)  评论(0编辑  收藏  举报