__call、__set 和 __get的用法

1. __call的用法

PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法。如果你试着调用一个对象中不存在的方法,__call 方法将会被自动调用。

例:__call

Php代码  收藏代码
  1. <?php  
  2. class foo {  
  3.     function __call($name,$arguments) {  
  4.         print("Did you call me? I'm $name!<br>");  
  5.         print_r($arguments);  
  6.         print("<br><br>");  
  7.     }  
  8.   
  9.     function doSecond($arguments)  
  10.     {  
  11.         print("Right, $arguments!<br>");  
  12.     }  
  13. }   
  14.    
  15. $test = new foo();  
  16. $test->doFirst('no this function');  
  17. $test->doSecond('this function exist');  
  18. ?>  

2. __call 实现“过载”动作

 这个特殊的方法可以被用来实现“过载(overloading)”的动作,这样你就可以检查你的参数并且通过调用一个私有的方法来传递参数。

例:使用 __call 实现“过载”动作

Php代码  收藏代码
  1. <?php  
  2. class Magic {  
  3.     function __call($name,$arguments) {  
  4.         if($name=='foo') {  
  5.             if(is_int($arguments[0])) $this->foo_for_int($arguments[0]);  
  6.             if(is_string($arguments[0])) $this->foo_for_string($arguments[0]);  
  7.         }  
  8.     }     
  9.       
  10.     private function foo_for_int($x) {  
  11.         print("oh an int!");  
  12.     }     
  13.           
  14.     private function foo_for_string($x) {  
  15.         print("oh a string!");  
  16.     }  
  17. }   
  18.   
  19. $test = new Magic();  
  20. $test->foo(3);  
  21. $test->foo("3");  
  22. ?>  

3.  面向对象重载

Php代码  收藏代码
  1. <?php  
  2. /*__call(string $name,array $arg)的用法 
  3. *当调用一个不可访问的方法的时候调用$name是方法名称 $arg是个数组包含要传递给方法的参数 
  4. */  
  5. class Account{  
  6.     private $user=1;  
  7.     private $pwd=2;  
  8.     public function __call($name,$arg){  
  9.         switch(count($arg)){  
  10.             case 2:  
  11.                 echo $arg[0]*$arg[1],PHP_EOL;  
  12.                 break;  
  13.             case 3:  
  14.                 echo array_sum($arg),PHP_EOL;  
  15.                 break;  
  16.             default:  
  17.                 echo "参数不对",PHP_EOL;  
  18.                 break;  
  19.         }  
  20.     }  
  21.   
  22. }  
  23.     $a= new Account();  
  24.     //这里模拟了重载  
  25.     //重载:一个类中可以定义参数列表不同但名字相同的多个方法  
  26.     $a->make(5);  
  27.     $a->make(5,6);  

 

4. 使用__call()方法来实现数据库连贯操作

Php代码  收藏代码
  1. <?php  
  2.   // 使用__call()方法来实现数据库连贯操作  
  3.  // 申明一个Db类(数据库操作类)的简单操作模型  
  4.   
  5. class Db{  
  6.     private $sql = array(  
  7.         "field" => "",  
  8.         "where" => "",  
  9.         "order" => "",  
  10.         "limit" => "",  
  11.         "group" => "",  
  12.         "having" => "",  
  13.     );  
  14.      
  15.     // 连贯操作调用field() where() order() limit() group() having()方法,组合sql语句  
  16.     function __call($methodName,$args){  
  17.         // 将第一个参数(代表不存在方法的方法名称),全部转成小写方式,获取方法名称  
  18.         $methodName = strtolower($methodName);  
  19.          
  20.         // 如果调用的方法名和成员属性数组$sql下标对应上,则将第二个参数给数组中下标对应的元素  
  21.         if(array_key_exists($methodName,$this->sql)){  
  22.             $this->sql[$methodName] = $args[0];  
  23.         }else{  
  24.             echo '调用类'.get_class($this).'中的方法'.$methodName.'()不存在';  
  25.         }  
  26.         // 返回自己对象,则可以继续调用本对象中的方法,形成连贯操作  
  27.         return $this;  
  28.     }  
  29.     // 输出连贯操作后组合的一个sql语句,是连贯操作最后的一个方法  
  30.     function select(){  
  31.         echo "SELECT {$this->sql['field']} FROM  user {$this->sql['where']} {$this->sql['order']} {$this->sql['limit']} {$this->sql['group']}  
  32.                 {$this->sql['having']}";  
  33.     }  
  34. }  
  35.   
  36. $db = new Db();  
  37.   
  38. // 连贯操作  
  39. $db->field('sex, count(sex)')  
  40.    ->where('where sex in ("男","女")')  
  41.    ->group('group by sex')  
  42.    ->having('having avg(age) > 25')  
  43.    ->select();  
  44. ?>  

5.  __set 和 __get的用法

这是一个很棒的方法,__set 和 __get 方法可以用来捕获一个对象中不存在的变量和方法。

例: __set 和 __get

Php代码  收藏代码
  1. <?php  
  2. class foo {  
  3.     function __set($name,$val) {  
  4.         print("Hello, you tried to put $val in $name<br>");  
  5.     }  
  6.    
  7.     function __get($name) {  
  8.         print("Hey you asked for $name<br>");  
  9.     }  
  10. }  
  11.   
  12. $test = new foo();  
  13. $test->__set('name','justcoding');  
  14. $test->__get('name');  
  15. ?>  
原文地址:https://www.cnblogs.com/DaBing0806/p/4720825.html