Home >Backend Development >PHP Tutorial >Detailed explanation of PHP5 object-oriented - (10) __set() __get() __isset() __unset() four methods_PHP tutorial
This article briefly introduces the detailed explanation of PHP5 object-oriented - (10) __set() __get() __isset() __unset() four methods. Friends who need help can refer to it.
__set() __get() __isset() __unset() Application of four methods
Generally speaking, always define class attributes as private, which is more in line with realistic logic. However, reading and assigning operations to attributes are very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain and assign attributes, and "__isset" to check attributes. ()" and the method to delete attributes "__unset()".
In the previous section, we set and obtained methods for each attribute. PHP5 provides us with special methods for setting and obtaining values for attributes, "__set()" and "__get()" These two methods, these two methods do not exist by default, but are manually added to the class. Like the constructor method (__construct()), it will only exist if it is added to the class. You can add it in the following way. Of course, these two methods can also be added according to personal style:
The code is as follows | Copy code | ||||
|
get() method: This method is used to get the value of a private member attribute. It has one parameter. The parameter is passed in the name of the member attribute you want to get, and the obtained attribute value is returned. This method does not need to be called manually. It is automatically called when private properties are directly obtained. Because the private properties have been encapsulated, the value cannot be obtained directly (for example: "echo $p1->name" is wrong to obtain directly), but if you add this method to the class, use " When a statement like echo $p1->name" directly obtains the value, it will automatically call the __get($property_name) method and pass the property name to the parameter $property_name. Through the internal execution of this method, the private value we passed in will be returned. The value of the attribute.
__set() method: This method is used to set values for private member attributes. It has two parameters. The first parameter is the name of the attribute you want to set the value for, and the second parameter is the value you want to set for the attribute. , no return value. This method also does not need to be called manually. It is automatically called when directly setting the private attribute value. The same private attribute has been encapsulated. If there is no __set() method, it is not allowed. For example: "$ this->name='zhangsan' , this will cause an error, but if you add the __set($property_name, $value) method to the class, it will be automatically called when you directly assign a value to the private property. Pass attributes such as name to $property_name, and pass the value "zhangsan" to be assigned to $value. Through the execution of this method, the purpose of assignment is achieved. 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 //__get() method is used to obtain private attributes //__set() method is used to set private attributes $p1=new Person(); //When directly assigning values to private attributes, the __set() method will be automatically called to assign values //Get the value of the private attribute directly, the __get() method will be automatically called to return the value of the member attribute |
Program execution result:
When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute
When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute
When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute
When directly obtaining the private attribute value, the __get() method is automatically called
Name: Zhang San
When directly obtaining the private attribute value, the __get() method is automatically called
Gender: Male
When directly obtaining the private attribute value, the __get() method is automatically called
Age: 20
If the above code does not add the __get() and __set() methods, the program will go wrong, because private members cannot be operated outside the class, and the above code automatically calls __get() and __set () method to help us directly access the encapsulated private members.
__isset() method: Before looking at this method, let’s take a look at the application of isset() function. isset() is a function used to determine whether a variable is set. Pass in a variable as a parameter. If the passed in variable exists, then Returns true, otherwise returns false.
So if you use the "isset()" function outside an object to determine whether the members inside the object are set, can you use it? There are two situations. If the members in the object are public, we can use this function to measure the member attributes. If they are private member attributes, this function will not work. The reason is that the private ones are encapsulated and are not exposed externally. Invisible. So we can't use the "isset()" function outside the object to determine whether the private member properties are set? Yes, you just need to add a "__isset()" method to the class. When the "isset()" function is used outside the class to determine whether the private members in the object are set, it will be automatically called inside the class. The "__isset()" method helps us complete such operations, and the "__isset()" method can also be made private. You can just add the following code to the class:
The code is as follows
|
Copy code | ||||
private function __isset($nm)
{
echo "When the isset() function is used outside the class to determine the private member $nm, it is automatically called";
Return isset($this->$nm);
}
private function __isset($nm)
{
echo "Automatically called when the isset() function is used outside the class to determine the private member $nm";
return isset($this->$nm);
}
__unset() method: Before looking at this method, let’s take a look at the "unset()" function first. The function of "unset()" is to delete the specified variable and return true. The parameters are deleted variable. So if you want to delete the member attributes inside the object outside an object, can you use the "unset()" function? There are two situations. If the member attributes inside an object are public, you can use this function to delete them outside the object. The public attributes of the object. If the member attributes of the object are private, I will not have the permission to delete them using this function. But similarly, if you add the "__unset()" method to an object, you can delete it from outside the object. Private member properties of the object. After adding the "__unset()" method to the object, when using the "unset()" function outside the object to delete the private member attributes inside the object, the "__unset()" function is automatically called to help us delete the private member attributes inside the object. Member attribute, this method can also be defined as private inside the class. Just add the following code to the object:
These four methods are added to the object and automatically called when needed to complete the operation of the private properties inside the object outside the object |