In general, it is more logical to define the attributes of a class as private. 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:
__get () method to get private property
code is as follows |
copy code |
Private function __get ($property _name) { if (isset ($this-> $property _name)) { return ($this-> $property _name); }else { return (NULL); } } //__set () method to set private properties Private Function __set ($property _name, $value) { $this-> $property _name = $value; } |
__get () Method: This method is used to get the private member property value, there is a parameter, the parameter passed to the name of the member property you want to get, return the obtained attribute value, this method does not need us to call by hand, because we can also make this method private method, object is automatically invoked when a private property is obtained directly. 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 calls the __get ($property _name) method, passes the property name to the parameter $property_name, and returns the value of the private property we passed in through the internal execution of the 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 have to call us manually, it can be made private, is the direct setting of private property value automatically when the call, the same property private has been encapsulated
, if there is no __set () This method is not allowed, such as: $this->name= ' Zhangsan ', this will make an error, but if you add __set ($property _name, $value) in the class, When assigning a value directly to a private property, it is automatically invoked, the attribute such as name is passed to $property_name, the value to be assigned "Zhangsan" is passed to the $value, and the purpose of the assignment is achieved through the execution of this method. 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. The code is as follows:
The code is as follows |
Copy Code |
<?php Class Person { The following are the member properties of the person, all of which are private members of the package Private $name; The name of a man Private $sex; The gender of the person Private $age; The age of the person __get () method to get private property Private Function __get ($property _name) { echo automatically invokes this __get () method <br> when directly retrieving private property values; if (Isset ($this-> $property _name)) { Return ($this-> $property _name); } Else { return (NULL); } } The __set () method is used to set private properties Private Function __set ($property _name, $value) { echo automatically invokes this __set () method to assign a value to a private property <br> "When setting the value of a private property directly;" $this-> $property _name = $value; } } $p 1=new person (); An action that assigns a value directly to a private property automatically calls the __set () method to assign the value $p 1->name= "John"; $p 1->sex= "Male"; $p 1->age=20; Gets the value of the private property directly, the __get () method is automatically called, and the value of the member property is returned echo "Name:". $p 1->name. " <br> "; echo "Gender:". $p 1->sex. " <br> "; echo "Age:" $p 1->age. " <br> "; ?> |
Program execution results:
When a private property value is set directly, this __set () method is automatically invoked to assign a value to a private property
, which automatically invokes the __set () method to assign a private property value
When setting private property values directly, This __set () method is automatically invoked to assign a value to a private property
automatically invokes this __get () method
name when directly fetching a private property value: John
automatically invokes this __get () method
Gender when directly fetching private property values: Male
The __get () method is automatically invoked when a private property value is obtained directly
Age:
If the above code does not add the __get () and __set () methods, the program will go wrong because the private member cannot be manipulated outside the class. 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 of 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. So 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:
The code is as follows |
Copy Code |
Private Function __isset ($NM) {
echo "Automatically invokes <br> when using the Isset () function outside the class to determine the private member $nm;
return 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 to the object, the "__unset ()" function is automatically invoked when the "unset ()" function is used outside the object to delete the private member properties inside the object.
We delete the private member properties within the object, and this method can also be defined as private within the class. Add the following code to the object:
The code is as follows |
Copy Code |
Private Function __unset ($NM) { echo "<br>" automatically invoked when using the unset () function outside the class to delete private members; unset ($this-> $nm); } Let's look at a complete example: <?php Class Person { The following are the member properties of the person Private $name; The name of a man Private $sex; The gender of the person Private $age; The age of the person __get () method to get private property Private Function __get ($property _name) { if (Isset ($this-> $property _name)) { Return ($this-> $property _name); }else { return (NULL); } } The __set () method is used to set private properties Private Function __set ($property _name, $value) { $this-> $property _name = $value; } __isset () method Private Function __isset ($NM) { The echo "Isset () function, when determining private members, automatically invokes <br>"; return Isset ($this-> $nm); } __unset () method Private Function __unset ($NM) { echo "<br>" automatically invoked when using the unset () function outside the class to delete private members; unset ($this-> $nm); } } $p 1=new person (); $p 1->name= "This was a person name"; When you use the Isset () function to determine a private member, the __isset () method is automatically invoked to help us complete, returning the result to True Echo Var_dump (Isset ($p 1->name)). " <br> "; echo $p 1->name. " <br> "; When you delete a private member using the unset () function, the __unset () method is automatically invoked to help us complete, delete the name private property unset ($p 1->name); has been deleted, and there is no output for this line. Echo $p 1->name; ?> |
The output results are:
Isset () function to determine a private member, automatically call the
BOOL (TRUE)
This is a person name
Called automatically when the unset () function is used outside of a class to delete a private member.
__set (), __get (), __isset (), __unset () These four methods are all added to the object, automatically invoked when needed, to complete the manipulation of private properties within the object outside the object