SoFunction
Updated on 2025-04-09

PHP object-oriented strategy (VI) Usage of __set __get __isset __unset

10. Application of four methods: __set() __get() __isset() __unset()
Generally speaking, the attributes of a class are always defined as private, which is more in line with the logic of reality. However, reading of properties
The sum assignment operation is very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain
Get and assign its attributes, as well as check the attribute's "__isset()" and delete the attribute's method "__unset()".
In the previous section, we made methods to set and obtain each attribute, and provided us with a special generic in PHP5.
The method of setting and getting values ​​in a sexual manner, "__set()" and "__get()" are two methods. These two methods do not exist by default.
Instead, we manually add it to the class, just like the constructor (__construct()), it will exist only if it is added to the class.
You can add these two methods in the following way, and of course you can also add them in your personal style:
Code snippet
Copy the codeThe code is as follows:

//_get() method is used to obtain private attributes
private function __get($property_name){
if(isset($this->$property_name)) {
return($this->$property_name);
}else {
return(NULL);
}
//_set() method is used to set private attributes
private function __set($property_name, $value){
$this->$property_name = $value;
}

__get() method: This method is used to obtain the property value of a private member. There is a parameter passed to the parameter you want to obtain.
The name of the member attribute returns the obtained attribute value. This method does not need to be called manually, because we can also
This method is made into a private method, and the object is automatically called when it directly obtains private properties. Because the private attributes have
After being encapsulated, the value cannot be directly obtained (for example, it is wrong to directly obtain "echo $p1->name").
But if you add this method to the class, use a statement like "echo $p1->name" to directly get the value
At this time, the __get($property_name) method will be automatically called, and the property name is passed to the parameter $property_name. Through the internal execution of this method, the value of the private property we passed in is returned. If the member attribute is not encapsulated as private, the object
This method will not be automatically called.
__set() method: This method is used to set values ​​for private member attributes. There are two parameters, the first parameter is for you to
For the attribute name of the set value, the second parameter is the value to be set for the attribute, and no return value is returned. This method doesn't require us to do this
Call it manually, it can also be made private, and it is automatically called when setting the private attribute value directly. The same attribute
Private ones have been encapsulated. Without the __set() method, it is not allowed, for example:
$this->name='zhangsan', this will make an error, but if you add __set($property_name, $value) to the class
This method will automatically call it when directly assigning values ​​to private attributes and pass the attributes such as name to
$property_name, pass the value "zhangsan" to the $value, and achieve the assignment through the execution of this method.
of. If the member attributes are not encapsulated as private, the object itself will not automatically call this method. In order not to pass it in illegal
You can also make a judgment on the value of . The code is as follows: Code snippet
Copy the codeThe code is as follows:

<?php
class Person{
//The following are the member attributes of people, all of which are encapsulated private members.
private $name; //The name of the person
private $sex; //People's gender
private $age; //People's age
//_get() method is used to obtain private attributes
private function __get($property_name){
echo "When directly obtaining the private attribute value, the __get() method is automatically called<br>";
if(isset($this->$property_name)) {
return($this->$property_name);
}else {
return(NULL);
}
}
//_set() method is used to set private attributes
private function __set($property_name, $value){
echo "When setting the private attribute value directly, the __set() method is automatically called to assign a value to the private attribute<br>";
$this->$property_name = $value;
}
}
$p1=new Person();
//The operation to directly assign values ​​to private attributes will automatically call the __set() method for assignment
$p1->name="Zhang San";
$p1->sex="male";
$p1->age=20;
//Turnly get the value of the private attribute, the __get() method will be automatically called to return the value of the member attribute
echo "Name:".$p1->name."<br>";
echo "Gender:".$p1->sex."<br>";
echo "Age:".$p1->age."<br>";
?>

Program execution results:
When setting the private attribute value directly, the __set() method is automatically called to assign values ​​to the private attribute. When setting the private attribute value directly, the __set() method is automatically called to assign values ​​to the private attribute.
When setting the private attribute value directly, 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 is not added with __get() and __set() methods, the program will make an error because it cannot operate privately outside the class.
There are members, and the above code helps us directly access the encapsulated private ones by automatically calling the __get() and __set() methods.
Member's.
__isset() method: Before looking at this method, let us look at the application of the "isset()" function. isset() is a function used to determine whether the variable is set. Pass a variable as a parameter. If the passed variable exists, it will be passed back true, otherwise it will be passed back false.
Then if you use the "isset()" function outside an object to determine whether the members in the object are set or not
What about using it? There are two situations. If the members in the object are public, we can use this function to determine the membership
Sex, if it is a private member attribute, this function will not work, because the private one is encapsulated, outside
The part is invisible. Then we can't
Use the "isset()" function outside the object to determine whether the private member attribute is set? Yes, you just need
Just add a "__isset()" method into the class. When using the "isset()" function outside the class to determine the object
When the private member of the face is set, the "__isset()" method in the class will be automatically called to help us complete this operation.
The "__isset()" method can also be made private. You can add the following code into the class:
Code snippet
Copy the codeThe code is as follows:

private function __isset($nm){
echo "Automatically call when using isset() function to determine private member $nm outside the class <br>";
return isset($this->$nm);
}

__unset() method: Before looking at this method, let’s take a look at the function “unset()” and “unset()”
The function of this function is to delete the specified variable and pass back true, and the parameter is the variable to be deleted. Then if in an object
Can I use the "unset()" function to delete the member attributes inside the object externally? There are also two situations:
The member attributes inside the object are public, you can use this function to delete the public attributes of the object outside the object.
The member attributes of the object are private, and I use this function and do not have permission to delete it, but also if you are in an object
Adding the method "__unset()" to delete the private member attributes of the object outside the object. In object
After adding the method "__unset()" to it, use the "unset()" function outside the object to delete the private inside the object.
When a member attribute is used, the "__unset()" function is automatically called to help us delete the private member attributes inside the object. This method can also be defined as private within the class. Just add the following code to the object:
Code snippet
Copy the codeThe code is as follows:

private function __unset($nm){
echo "It is called automatically when using the unset() function outside the class to delete private members<br>";
unset($this->$nm);
}

Let's look at a complete example:
Code snippet
Copy the codeThe code is as follows:

<?php
class Person{
//The following are the member attributes of people
private $name; //The name of the person
private $sex; //People's gender
private $age; //People's age
//_get() method is used to obtain private attributes
private function __get($property_name){
if(isset($this->$property_name)){
return($this->$property_name);}else {
return(NULL);
}
}
}
//_set() method is used to set private attributes
private function __set($property_name, $value){
$this->$property_name = $value;
}
//_isset() method
private function __isset($nm){
echo "isset() function automatically calls <br>";
return isset($this->$nm);
}
//_unset() method
private function __unset($nm){
echo "It is called automatically when using the unset() function outside the class to delete private members<br>";
unset($this->$nm);
}
}
$p1=new Person();
$p1->name="this is a person name";
//When using the isset() function to measure private members, the __isset() method is automatically called to help us complete, and the return result is true
echo var_dump(isset($p1->name))."<br>";
echo $p1->name."<br>";
//When using the unset() function to delete private members, the __unset() method is automatically called to help us complete and delete the name private attribute
unset($p1->name);
//It has been deleted, so there will be no output in this line
echo $p1->name;
?>

The output result is:
When the isset() function determines a private member, it is automatically called
bool(true)
this is a person name
Automatically called when using the unset() function outside the class to delete private members
__set(), __get(), __isset(), and __unset() are all added to the object, and when needed
Automatically call to complete the operation of private properties inside the object outside the object.