__set (), __get (), __isset (), __unset () method of Use

Source: Internet
Author: User

. __set () __get () __isset () __unset () FourMethodIn general, the attribute of the class is always defined as private, which is more in line with the logic of reality. However, read and assign operations on attributes are very frequent, therefore, in PHP5, two functions "__get ()" and "__set ()" are predefined to get and assign their properties, and to check the "__isset ()" of the property and the Method "__unset ()" for deleting the property. In the previous section, we set up and get a method for each property, in PHP5 we provide methods for setting values and fetching values for attributes, "__set ()" and "__get ()" methods, which are not by default, but rather as we add them to the class by hand, such as the constructor method ( __construct ()), just as the class is added to exist, you can add the two methods in the following way, and of course you can add them in a personal style: the//__get () method is used to get the private property, __get ($ property_ Name {if (Isset ($ this-> $ property_name)) {return ($ this-> $ property_name);} else {return (NULL); The//__set () method is used to set the private property, __set ($ property_name, $ value) {$ this-> $ property_name = $ value;} __get () Method: This method is used to get private member property values. There is a parameter that passes in the name of the member property you want to get, and returns the property value obtained, which is not to be invoked by hand, because we can also make the method private by making the private property directly availableObjectAutomatically invoked. Because private properties are already encapsulated, it is not possible to get values directly (such as "Echo $p 1->name"), but if you add this method to the class, you use the "Echo $p 1->name" When such a statement gets the value directly, it automatically invokes the __get ($property _name) method, passing the property name to the parameter $property _name, and returns the value of the private property we passed in, through the internal execution of this method. If a member property is not encapsulated as private, the object itself does not automatically invoke this method. __set () Method: This method is used to set the value for the private member property, with two arguments, the first argument is the property name you want to set the value for, and the second parameter is the value to be set for the property, and there is no return value. This method also does not need us to call manually, it can also be made private, is to set the private property value directly when the call automatically, the same attributes private has been encapsulated, if there is no __set () This method, is not allowed, such as: $this->name= ' Zhangsan ' This can go wrong, but if you add a __set ($property _name, $value) to the class, you automatically call it when you assign a value to the private property, and you pass the attribute such as name to the $property _name, and the value you want to assign Zhangsan "to the $value, through the execution of this method, to achieve the purpose of the assignment. If a member property is not encapsulated as private, the object itself does not automatically invoke this method. In order to not pass the illegal value, you can also make a judgment in this method.CodeAs follows: <?php class  person {//the following is a member property of a human, all of which is a private member of the wrapper private $ name;       //person's name PR Ivate $ sex;        //person's sex private $ age;        //person's age//__get () method is used to get private properties function  __get ($ property_name) { echo automatically invokes this __get () method <br> when directly retrieving private property values; if (Isset ($ this-> $ property_name)) {return ($ A-> $ property_name);} else {return (NULL);}} The __set () method is used to set the private properties private function  __set ($ property_name, $ value) {echo automatically invokes this __set () method when setting private property values directly as private have attribute assignment <br> "; $ this-> $ property_name = $ value; } $ p1 = new person (); An operation that assigns a value directly to a private property automatically calls the __set () method to assign the value $ p1->name= "John"; $ p1->sex= "male"; $ P1->age= 20; Getting the value of a private property directly invokes the __get () method, returning the value of the member property, echo "Name:". $ P1->name. "<br>"; echo "Sex:". $ P1->sex. "<br>"; echo "Age:". $ P1->age. "<br>";?> program Execution results: This __set () method is automatically invoked to assign a value to a private property when the private property value is set directly
This __set () method is automatically invoked to assign a value to a private property when the private property value is set directly
This __set () method is automatically invoked to assign a value to a private property when the private property value is set directly
This __get () method is automatically invoked when a private property value is obtained directly
Name: John
This __get () method is automatically invoked when a private property value is obtained directly
Sex: Male
This __get () method is automatically invoked when a private property value is obtained directly
Age: 20 Above code if you do not add the __get () and __set () methods, the program will make an error because the private member cannot be manipulated outside of the class, and the code above is to help us directly access the encapsulated private members by automatically invoking the __get () and __set () methods. __isset () Method: Before looking at this method, let's look at the application of the "isset ()" function, Isset () is a function to determine whether a variable is set, pass in a variable as a parameter, return TRUE if the passed variable exists, or return false. So if you use the "isset ()" function outside an object to determine if the members of the object are set, can you use it? In two cases, if the object inside the member is public, we can use this function to determine the member property, if it is a private member property, this function does not work, because the private is encapsulated, not visible outside. Then we can not use the "isset ()" function outside the object to determine whether the private member property is set. Yes, you just add a "__isset" method to the class. When a "isset ()" function is used outside of a class to determine whether a private member within an object is set, the "__isset ()" Method inside the class is automatically invoked to help us accomplish such an operation, "__ The Isset () method can also be made private. You can add the following code to the class: Private function  __isset ($ nm) {echo "automatically invokes <br> when using the Isset () function outside the class to determine $nm for private members; re Turn isset ($ this-> $ nm); __unset () Method: Before we look at this method, we'll take a look at the "unset ()" function, "unset ()" function is to delete the specified variable and return True, the argument is the variable to be deleted. So if you delete the member properties inside an object outside of an object with the "unset ()" function, is also divided into two cases, if the member property inside an object is public, you can use this function to delete the object's public properties outside the object, if the object's member property is private, I use this function without permission to delete it, but also if you add the "__unset ()" method to an object, you can delete the object's private member properties outside of the object. After adding the "__unset ()" Method inside the object, the objectExternal use of the unset () function to delete private member properties within an object automatically calls the "__unset ()" function to help us delete private member properties within the object, which can also be defined as private within the class. Add the following code to the object: Private function  __unset ($ nm) {echo] automatically invokes <br> when the unset () function is used outside the class to delete private members; Unset ($ This-> $ nm); Let's look at a complete example: <?php class Person {

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.