__construct 构造方法
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();
__destruct 析构方法
1 public function __destruct(){
2 echo "
{$this->name}被清理
";3 }
4
5 # 后进先出 (弹夹原理)
__toString 对象说明
class Usb
{
public function load(){
echo "
load
";}
public function __toString(){
return 'This is a obj';
}
}
$obj = new Usb();
echo $obj;
__call 访问对象不存在的方法时调用
1 class Usb
2 {
3 public function load(){
4 echo "
load
";5 }
6
7 public function __call($i,$j){
8 return "
您访问的方法{$i}不存在
";9 }
10 }
11
12 $obj = new Usb();
13 echo $obj->start();
__get 提示无权限获取的方法
1 class Usb
2 {
3 private function load(){
4 echo "
load
";5 }
6
7 public function __get($i){
8 return "
您无权访问{$i}方法
";9 }
10 }
11
12 $obj = new Usb();
13 echo $obj->load;
__set 提示无权限设置的方法
1 class Usb
2 {
3 private $name = 'yu';
4
5 public function __set($i,$j){
6 echo "
您无权设置{$i}为{$j}
";7 }
8 }
9
10 $obj = new Usb();
11 $obj->name = 'yach';
__isset 检测私有属性值是否被设定
1 class Usb
2 {
3 private $name = 'yu';
4
5 public function __isset($i){
6 return "
未设定{$i}属性
";7 }
8 }
9
10 $obj = new Usb();
11 $b = isset($obj->name);
12 echo $b;
__unset 无权删除提示
1 class Usb
2 {
3 private $name = 'yu';
4
5 public function __unset($i){
6 echo "
您无权删除{$i}属性
";7 }
8 }
9
10 $obj = new Usb();
11 unset($obj->name);