From php5 and later versions, classes can use magic methods. PHP stipulates that methods starting with two underscores (__) are reserved as magic methods, so it is recommended that function names do not start with __ unless it is to overload existing magic methods.
The existing magic methods in PHP include __construct, __destruct, __call, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __set_state and __clone.
This articleSlowly looking for the night, the bright moon hangs high in the sky
__isset() - When using the isset() method on a class attribute or a non-class attribute, if there is no or non-public attribute, the __isset() method will be automatically executed.
__unset() -When using the unset() method on a class attribute or a non-class attribute, if there is no or non-public attribute, the __unset() method will be automatically executed
public = 'pub'; $this->protected = 'pro'; $this->private = 'pri'; } public function __isset($var){ echo '这里通过__isset()方法查看属性名为 '.$var."\n"; } public function __unset($var){ echo '这里通过__unset()方法要销毁属性名为 '.$var."\n"; } } $exa = new Example; echo ''; var_dump(isset($exa->public)); echo "\n"; var_dump(isset($exa->protected)); echo "\n"; var_dump(isset($exa->private)); echo "\n"; var_dump(isset($exa->noVar)); echo "\n"; echo '
'; unset($exa->public); var_dump($exa); echo "\n"; unset($exa->protected); echo "\n"; unset($exa->private); echo "\n"; unset($exa->noVar); echo "\n";Copy after login
The result is as follows:
bool(true) 这里通过__isset()方法查看属性名为 protected bool(false) 这里通过__isset()方法查看属性名为 private bool(false) 这里通过__isset()方法查看属性名为 noVar bool(false) ------------------------------------------------------------------------------ object(Example)#1 (2) { ["protected:protected"]=> string(3) "pro" ["private:private"]=> string(3) "pri" } 这里通过__unset()方法要销毁属性名为 protected 这里通过__unset()方法要销毁属性名为 private 这里通过__unset()方法要销毁属性名为 noVar
The above introduces the PHP magic methods: __isset and __unset, including the content of PHP magic methods. I hope it will be helpful to friends who are interested in PHP tutorials.