标签:name des fun 原理 function 对象 调用 set 构造方法
1 class Person 2 { 3 public $name; 4 5 # 构造方法 6 public function __construct($n){ 7 $this->name = $n; 8 } 9 10 public function say(){ 11 return "my name is {$this->name}"; 12 } 13 } 14 15 $obj = new Person(‘yach‘); 16 echo $obj->say();
1 public function __destruct(){ 2 echo "<p>{$this->name}被清理</p>"; 3 } 4 5 # 后进先出 (弹夹原理)
class Usb { public function load(){ echo "<p>load</p>"; } public function __toString(){ return ‘This is a obj‘; } } $obj = new Usb(); echo $obj;
1 class Usb 2 { 3 public function load(){ 4 echo "<p>load</p>"; 5 } 6 7 public function __call($i,$j){ 8 return "<p>您访问的方法<b>{$i}</b>不存在</p>"; 9 } 10 } 11 12 $obj = new Usb(); 13 echo $obj->start();
1 class Usb 2 { 3 private function load(){ 4 echo "<p>load</p>"; 5 } 6 7 public function __get($i){ 8 return "<p>您无权访问{$i}方法</p>"; 9 } 10 } 11 12 $obj = new Usb(); 13 echo $obj->load;
1 class Usb 2 { 3 private $name = ‘yu‘; 4 5 public function __set($i,$j){ 6 echo "<p>您无权设置{$i}为{$j}</p>"; 7 } 8 } 9 10 $obj = new Usb(); 11 $obj->name = ‘yach‘;
1 class Usb 2 { 3 private $name = ‘yu‘; 4 5 public function __isset($i){ 6 return "<p>未设定{$i}属性</p>"; 7 } 8 } 9 10 $obj = new Usb(); 11 $b = isset($obj->name); 12 echo $b;
1 class Usb 2 { 3 private $name = ‘yu‘; 4 5 public function __unset($i){ 6 echo "<p>您无权删除{$i}属性</p>"; 7 } 8 } 9 10 $obj = new Usb(); 11 unset($obj->name);
标签:name des fun 原理 function 对象 调用 set 构造方法
原文地址:https://www.cnblogs.com/yachyu/p/10708234.html