We often see bits __set __get __isset __unset The usage of these things in the object-oriented PHP, but I don't understand why we use them, so let's introduce the usage of their brother four.
In general, it is more realistic logic to always define the properties of a class as private. However, the read and assign operations for the properties are very frequent, so in PHP5, two functions "__get ()" and "__set ()" are predefined to get and assign their properties, as well as to check the Properties ' __isset () and delete the properties "__unset ()".
In the previous section, we set and get a method for each property, and in PHP5 we were given a way to set the value and get the value specifically for the property, "__set ()" and "__get ()", two methods that were not by default, but that we manually added to the class, like the construction method ( __construct ()), the class is added in order to exist, you can add the two methods as follows, of course, can also be added according to personal style:
The __get () method is used to get the private property
| |
copy code |
| Private Function __get ($property _name) { if (isset ($this-$property _name) { return ($this-$property _name); }else { return (NULL); } } //__set () method used to set private properties Private Function __set ($property _name, $value) { $this-$property _name = $value; } |
__get () Method: This method is used to get the value of the private member property, there is a parameter, the parameter passed in the name of the member property you want to get, return the obtained property value, this method does not have to be called by us manually, because we can also make this method private method, The object is automatically called when the private property is obtained directly. Because the private property is already encapsulated, it is not possible to get the value directly (for example: "Echo $p 1->name"), but if you add this method to the class, use "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 our incoming private property through the internal execution of the method. If the member property is not encapsulated as private, the object itself does not automatically call this method.
__set () Method: This method is used to set the value of the private member property, there are two parameters, the first parameter is you want to set the value of the property name, the second parameter is to set the value of the property, there is no return value. This method does not have to be called manually, it can also be made private, is directly set the value of the private property is automatically invoked, the same property private has been encapsulated
, if there is no __set () This method is not allowed, such as: $this->name= ' Zhangsan ', this will be wrong, but if you add __set in the Class ($property _name, $value) This method, When assigning a value directly to a private property, it is automatically invoked, passing the attribute, such as name, to the $property_name, passing the value "Zhangsan" to the $value, which is executed to achieve the purpose of the assignment. If the member property is not encapsulated as private, the object itself does not automatically call this method. In order not to pass in illegal values, you can also make a judgment in this method. The code is as follows:
| The code is as follows |
Copy Code |
Class Person
{
The following are the member properties of a person, both encapsulated private members
Private $name; The name of the man
Private $sex; Man's Sex
Private $age; The age of the person
The __get () method is used to get the private property
Private Function __get ($property _name)
{
echo "Automatically calls this __get () method when it directly acquires the value of a private property. ";
if (Isset ($this-$property _name))
{
Return ($this, $property _name);
}
Else
{
return (NULL);
}
}
The __set () method is used to set the private property
Private Function __set ($property _name, $value)
{
echo "Automatically calls this __set () method to assign a value to a private property when setting the value of a private property directly ";
$this $property _name = $value;
}
}
$p 1=new person ();
An operation that assigns a value directly to a private property, automatically calls the __set () method to assign the value
$p 1->name= "Zhang San";
$p 1->sex= "Male";
$p 1->age=20;
Gets the value of the private property directly, automatically calls the __get () method, returns the value of the member property
echo "Name:". $p 1->name. " ";
echo "Gender:". $p 1->sex. " ";
echo "Age:" $p 1->age. " ";
?>
|
Program execution results:
automatically calls this __set () method to assign a private property value
When setting a private property value directly when setting private property values, automatically calls this __set () method to assign a private property
when setting a private property value directly, The __set () method is automatically called to assign a value to the private property
when the value of the private property is directly obtained, the __get () method
name is automatically called: Zhang San
automatically calls the __get () method when it directly acquires the value of the private property
Gender: Male The
automatically calls the __get () method when the value of the private property is obtained directly
Age:
The program will go wrong if the above code is not added with the __get () and __set () methods, because private members cannot be manipulated outside the class. The code above is automatically called by the __get () and __set () methods to help us directly access the encapsulated private members.
__isset () method: Before looking at this method, we 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, or return TRUE if the passed-in variable exists, or return false. So what if the "isset ()" function is used outside an object to determine if the members of the object are set, can it be used? In two cases, if the members of the object are public, we can use this function to determine the member properties, if it is a private member property, this function will not work, because the private is encapsulated, is not visible externally. Then we can not use the "isset ()" function outside the object to determine whether the private member property is set? Yes, you can just add a "__isset ()" method to the class, and when you use the "isset ()" function outside of the class to determine whether a private member of the object is set, the "__isset ()" Method inside the class is automatically invoked to help us do this, "__ The Isset () "method can also be made private. You can add the following code to the class:
| |
copy code |
| Private function __isset ($nm) { echo "Automatically calls " when using the Isset () function outside of a class to measure a private member $nm; Return Isset ($this-$nm); } |
__unset () Method: Before looking at this method, let's take a look at the "unset ()" function, the function of "unset ()" is to delete the specified variable and return True, the argument is the variable to be deleted. So if it is possible to delete the member property inside an object outside of an object with the "unset ()" function, it is also in two cases, if the member property inside an object is public, you can use this function to delete the public property of the object outside the object, if the object's member property is private, I don't have permission to delete this function, but also if you add "__unset ()" To an object, you can delete the object's private member properties outside the object. After adding "__unset ()" To the object, use the "unset ()" function outside the object to delete the private member property inside the object, automatically calling the "__unset ()" function to help
We delete the private member property inside the object, and this method can 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 "When you use the unset () function outside of a class to delete a private member, the automatically called ";
Unset ($this, $nm);
}
Let's look at a complete example: Class Person
{
The following is the person's member property
Private $name; The name of the man
Private $sex; Man's Sex
Private $age; The age of the person
The __get () method is used to get the 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 the private property
Private Function __set ($property _name, $value)
{
$this $property _name = $value;
}
__isset () method
Private Function __isset ($NM)
{
The echo "Isset () function automatically calls when a private member is measured ";
Return Isset ($this-$NM);
}
__unset () method
Private Function __unset ($NM)
{
echo "When you use the unset () function outside of a class to delete a private member, the automatically called ";
Unset ($this, $nm);
}
}
$p 1=new person ();
$p 1->name= "This was a person name";
When using the Isset () function to measure a private member, the __isset () method is automatically called to help us complete and return the result to True
Echo Var_dump (Isset ($p 1->name)). " ";
echo $p 1->name. " ";
When you use the unset () function to delete a private member, the __unset () method is called automatically to help us complete, removing the name private property
unset ($p 1->name);
has been removed, there is no output for this line
Echo $p 1->name;
?>
|
The output is:
The Isset () function automatically calls when a private member is measured
BOOL (TRUE)
This was a person name
Automatically called when a private member is removed by using the unset () function outside the class.
__set (), __get (), __isset (), __unset () are all four methods that we add to the object and are automatically called when needed to complete the operation of the object's internal private property outside the object.
http://www.bkjia.com/PHPjc/444688.html www.bkjia.com true http://www.bkjia.com/PHPjc/444688.html techarticle we often see bits __set __get __isset __unset The usage of these things in the object-oriented PHP, but I don't understand why we use them, so let's introduce one ...