与__isset()类似
未定义或无权限访问的属性上调用unset()方法时会触发__unset()方法。
示例代码如下:
<?php class Person { public $sex; private $name; private $age; public function __construct($name="", $age=25, $sex='Male') { $this->name = $name; $this->age = $age; $this->sex = $sex; } /** * @param $content * * @return bool */ public function __unset($content) { echo "It is called automatically when we use the unset() method outside the class.<br>"; echo isset($this->$content); } } $person = new Person("John", 25); // 赋初始值 unset($person->sex),"<br>"; unset($person->name),"<br>"; unset($person->age),"<br>";
输出结果如下:
It is called automatically when we use the unset() method outside the class. 1 It is called automatically when we use the unset() method outside the class. 1
登录后可发表评论