PHP magic methods _ ISSET and _ UNSET use instances __isset _ unset

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.