PHP magic methods _ ISSET and _ UNSET use instances __isset _ unset
_ Isset ()-if there is no or non-public attribute when the isset () method is used for attributes in the class or non-class, the _ isset () method is automatically executed.
_ Unset ()-When the unset () method is used for attributes in the class or non-class, if there is no or non-public attribute, the _ unset () method is automatically executed.
Copy codeThe Code is as follows:
<? Php
/**
* Examples of magic methods _ isset () and _ unset () in the class
*/
Class Example {
Public $ public;
Protected $ protected;
Private $ private;
Public function _ construct (){
$ This-> public = 'pub ';
$ This-> protected = 'pro ';
$ This-> private = 'pri ';
}
Public function _ isset ($ var ){
Echo 'Here, the attribute name is'. $ var. "\ n" through the _ isset () method ";
}
Public function _ unset ($ var ){
Echo 'here the name of the attribute to be destroyed is'. $ var. "\ n" through the _ unset () method ";
}
}
$ Exa = new Example;
Echo '<pre> ';
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 ";
The result is as follows:
Bool (true)
Here, the attribute name is protected through the _ isset () method.
Bool (false)
Here, the attribute name is private through the _ isset () method.
Bool (false)
Here, you can use the _ isset () method to view the attribute named noVar.
Bool (false)
Copy codeThe Code is as follows:
Object (Example) #1 (2 ){
["Protected: protected"] =>
String (3) "pro"
["Private: private"] =>
String (3) "pri"
}
The _ unset () method is used to destroy the attribute name protected.
Here, the name of the attribute to be destroyed is private through the _ unset () method.
The name of the attribute to be destroyed through the _ unset () method is noVar.