使用框架: ci。。。
参考官网信息
参考官网示例代码即可
1 <?php 2 class PropertyTest { 3 /** 被重载的数据保存在此 */ 4 private $data = array(); 5 6 7 /** 重载不能被用在已经定义的属性 */ 8 public $declared = 1; 9 10 /** 只有从类外部访问这个属性时,重载才会发生 */ 11 private $hidden = 2; 12 13 public function __set($name, $value) 14 { 15 echo "Setting '$name' to '$value'\n"; 16 $this->data[$name] = $value; 17 } 18 19 public function __get($name) 20 { 21 echo "Getting '$name'\n"; 22 if (array_key_exists($name, $this->data)) { 23 return $this->data[$name]; 24 } 25 26 $trace = debug_backtrace(); 27 trigger_error( 28 'Undefined property via __get(): ' . $name . 29 ' in ' . $trace[0]['file'] . 30 ' on line ' . $trace[0]['line'], 31 E_USER_NOTICE); 32 return null; 33 } 34 35 public function __isset($name) 36 { 37 echo "Is '$name' set?\n"; 38 return isset($this->data[$name]); 39 } 40 41 public function __unset($name) 42 { 43 echo "Unsetting '$name'\n"; 44 unset($this->data[$name]); 45 } 46 47 /** 非魔术方法 */ 48 public function getHidden() 49 { 50 return $this->hidden; 51 } 52 } 53 54 55 echo "<pre>\n"; 56 57 $obj = new PropertyTest; 58 59 $obj->a = 1; 60 echo $obj->a . "\n\n"; 61 62 var_dump(isset($obj->a)); 63 unset($obj->a); 64 var_dump(isset($obj->a)); 65 echo "\n"; 66 67 echo $obj->declared . "\n\n"; 68 69 echo "Let's experiment with the private property named 'hidden':\n"; 70 echo "Privates are visible inside the class, so __get() not used...\n"; 71 echo $obj->getHidden() . "\n"; 72 echo "Privates not visible outside of class, so __get() is used...\n"; 73 echo $obj->hidden . "\n"; 74 ?>
标签:__,obj,变量,成员,echo,return,php,data,name From: https://www.cnblogs.com/supermarx/p/17867649.html