Home >Backend Development >PHP Tutorial >Detailed explanation of PHP5 object-oriented - (10) __set() __get() __isset() __unset() four methods_PHP tutorial

Detailed explanation of PHP5 object-oriented - (10) __set() __get() __isset() __unset() four methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:17886browse

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()方法用来获取私有属性  
function __get($property_name)  
{  
    if(isset($this->$property_name)) {  
        return($this->$property_name);  
    }else {  
        return(NULL);  
    }  
}   
 
//__set()方法用来设置私有属性  
function __set($property_name, $value)  
{  
    $this->$property_name = $value;  

//__get()方法用来获取私有属性
function __get($property_name)
{
    if(isset($this->$property_name)) {
        return($this->$property_name);
    }else {
        return(NULL);
    }
}

//__set()方法用来设置私有属性
function __set($property_name, $value)
{
 $this->$property_name = $value;
}__

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

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
{
//The following are the member attributes of people, all of which are encapsulated private members
Private $name; //Person’s name
Private $sex; //Person’s gender
Private $age; //Age of person

//__get() method is used to obtain private attributes
Function __get($property_name)

                                                                                                                with with echo "When directly obtaining the private attribute value, this __get() method is automatically called
"; If(isset($this->$property_name)) {
                 return($this->$property_name);                                                                                                                                 return(NULL);                                                                                                                             }  

//__set() method is used to set private attributes
Function __set($property_name, $value)

                    echo "When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute
"; 
            $this->$property_name = $value;                                           }  
}

$p1=new Person();

//If you directly assign a value to a private attribute, the __set() method will be automatically called for assignment
$p1->name="Zhang San";
$p1->sex="Male";
$p1->age=20;

//Get the value of the private attribute directly, and the __get() method will be automatically called to return the value of the member attribute
echo "Name:".$p1->name."
";
echo "Gender:".$p1->sex."
";
echo "Age:".$p1->age."
";

class Person
{
//The following are the member attributes of people, all of which are encapsulated private members
Private $name; //Person’s name
Private $sex; //Person’s gender

Private $age; //Age of person

//__get() method is used to obtain private attributes
Function __get($property_name)
{
               echo "When directly obtaining the private attribute value, this __get() method is automatically called
";
If(isset($this->$property_name)) {
              return($this->$property_name);
         }else {
             return(NULL);
}
}

//__set() method is used to set private attributes
Function __set($property_name, $value)
{
echo "When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute
";
$this->$property_name = $value;
}
}

$p1=new Person();

//When directly assigning values ​​to private attributes, the __set() method will be automatically called to assign values
$p1->name="Zhang San";
$p1->sex="Male";
$p1->age=20;

//Get the value of the private attribute directly, the __get() method will be automatically called to return the value of the member attribute
echo "Name:".$p1->name."
";
echo "Gender:".$p1->sex."
";
echo "Age:".$p1->age."
";

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
 代码如下 复制代码

private function __isset($nm)  
{  
    echo "当在类外部使用isset()函数测定私有成员$nm时,自动调用";  
    return isset($this->$nm);  

private function __isset($nm)
{
 echo "当在类外部使用isset()函数测定私有成员$nm时,自动调用";
 return isset($this->$nm);
}

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:

The code is as follows Copy code

private function __unset($nm)
{
echo "Automatically called when the unset() function is used outside the class to delete a private member";
Unset($this->$nm);
}

private function __unset($nm)
{
echo "Automatically called when the unset() function is used outside the class to delete a private member";
unset($this->$nm);
} Let’s take a look at a complete example:


class Person
{
//The following are the member attributes of people
Private $name; // Person’s name
Private $sex; // Person’s gender
Private $age; // The person’s age
//__get() method is used to obtain private attributes
Private function __get($property_name)

If (isset($this->$property_name)) {
                 return($this->$property_name);                                                                                                                   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 "The isset() function is automatically called when measuring private members";                                                return isset($this->$nm);                              }  
// __unset() method
Private function __unset($nm)

echo "Automatically called when the unset() function is used outside the class to delete a private member";
         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 it, and the return result is true
echo var_dump(isset($p1->name)) . "";
echo $p1->name . "";
// When using the unset() function to delete private members, the __unset() method is automatically called to help us complete the task and delete the name private attribute
unset($p1->name);
// It has been deleted, so there will be no output for this line
echo $p1->name;

class Person
{
//The following are the member attributes of people
private $name; // person’s name
private $sex; // Person’s gender
private $age; // The person’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);
}
}
//The __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 "The isset() function is automatically called when measuring private members";
Return isset($this->$nm);
}
// __unset() method
private function __unset($nm)
{
echo "Automatically called when the unset() function is used outside the class to delete a private member";
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 it, and the return result is true
echo var_dump(isset($p1->name)) . "";
echo $p1->name . "";
// When using the unset() function to delete private members, the __unset() method is automatically called to help us complete the task and delete the name private attribute
unset($p1->name);
// has been deleted, so there will be no output for 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

When the isset() function determines a private member, it is automatically called
bool(true)
this is a person name __set(), __get(), __isset(), __unset()

are automatically called when the unset() function is used outside the class to delete private members.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629197.htmlTechArticleThis article briefly introduces the detailed explanation of PHP5 object-oriented - (10) __set() __get() __isset() There are four methods of __unset(). Friends who need help can refer to them. __set() __get() __i...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn