Home >Backend Development >PHP Tutorial >Introduction to the usage of __set __get __isset __unset in PHP object-oriented_PHP tutorial

Introduction to the usage of __set __get __isset __unset in PHP object-oriented_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:10:52759browse

We often see the usage of things like __set __get __isset __unset in PHP object-oriented, but we don’t understand why these things are used. Let’s introduce their usage one by one.

Generally speaking, always define the attributes of a class 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 their attributes, as well as "__isset" to check the 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:

//__get() method is used to obtain private attributes

Copy code
The code is as follows
 代码如下 复制代码

private function __get($property_name)


{

 


if(isset($this->$property_name))


{


return($this->$property_name);


}else


{


return(NULL);


}


}


//__set()方法用来设置私有属性


private function __set($property_name, $value)


{


$this->$property_name = $value;


}

private function __get($property_name){ if(isset($this->$property_name)){ return($this->$property_name);}else{return(NULL );}}//__set() method is used 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 attribute value. 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 is not used We call it manually, because we can also make this method a private method, which is automatically called by the object when the private property is 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, the __get($property_name) method will be automatically called, and the property name will be passed 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. If the member properties are not encapsulated as private, the object itself will not automatically call this method.
__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 can also be made private. It is automatically called when directly setting the private attribute value. The same private attribute has been encapsulated with

. If there is no __set () This method 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, you can directly When assigning a value to a private attribute, it will be called automatically, passing the attribute such as name to $property_name, and passing the value "zhangsan" to be assigned to $value. Through the execution of this method, the purpose of assignment is achieved. If the member properties are not encapsulated as private, the object itself will 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


{


//下面是人的成员属性, 都是封装的私有成员


private $name; //人的名子


private $sex; //人的性别


private $age; //人的年龄


//__get()方法用来获取私有属性


private function __get($property_name)


{


echo "在直接获取私有属性值的时候,自动调用了这个__get()方法
";


if(isset($this->$property_name))


{


return($this->$property_name);


}


else


{


return(NULL);


}


}


//__set()方法用来设置私有属性


private function __set($property_name, $value)


{


echo "在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值
";

 


$this->$property_name = $value;


}


}


$p1=new Person();


//直接为私有属性赋值的操作, 会自动调用__set()方法进行赋值


$p1->name="张三";


$p1->sex="男";


$p1->age=20;


//直接获取私有属性的值, 会自动调用__get()方法,返回成员属性的值


echo "姓名:".$p1->name."
";


echo "性别:".$p1->sex."
";


echo "年龄:".$p1->age."
";


?>

";if(isset($this->$property_name)){return($this->$property_name); }else{return(NULL); }}//__set() method is used to set private properties private function __set($property_name, $value){echo "When directly setting the value of a private property, this __set() method is automatically called to assign a value to the private property
"; $this->$property_name = $value;}}$p1=new Person();//When directly assigning a value to a private attribute, the __set() method will be automatically called for assignment $p1->name="张三";$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 attributeecho "Name:". $p1->name."
";echo "Gender:".$p1->sex."
";echo "Age:".$p1->age."
";?>

Program execution results:
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, this __set is automatically called. The () method assigns values ​​to private attributes
When directly setting the value of a private attribute, the __set() method automatically calls the __set() method to assign a value to a private attribute
When directly obtaining the value of a private attribute, the __get is automatically called () method
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 This __get() method is called
Age: 20

If the above code does not add the __get() and __set() methods, the program will error because private operations cannot be performed outside the class members, and the above code helps us directly access the encapsulated private members by automatically calling the __get() and __set() methods.
__isset() method: Before looking at this method, let’s take a look at the application of the “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 Returns true if it exists, 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 attributes have been 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 "当在类外部使用isset()函数测定私有成员$nm时,自动调用
";


return isset($this->$nm);


}

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);}

__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 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 object. Internal private member attribute, this method can also be defined as private inside the class. Just add the following code to the object:

echo "When in class Automatically called when the unset() function is used externally to delete private members
";
The code is as follows Copy code
 代码如下 复制代码

private function __unset($nm)


{


echo "当在类外部使用unset()函数来删除私有成员时自动调用的
";


unset($this->$nm);


}

我们来看一个完整的实例:


class Person


{


//下面是人的成员属性


private $name; //人的名子


private $sex; //人的性别


private $age; //人的年龄


//__get()方法用来获取私有属性


private function __get($property_name)


{


if(isset($this->$property_name))


{


return($this->$property_name);


}else {


return(NULL);


}


}


//__set()方法用来设置私有属性


private function __set($property_name, $value)


{

 


$this->$property_name = $value;


}


//__isset()方法


private function __isset($nm)


{


echo "isset()函数测定私有成员时,自动调用
";


return isset($this->$nm);


}


//__unset()方法


private function __unset($nm)


{


echo "当在类外部使用unset()函数来删除私有成员时自动调用的
";


unset($this->$nm);


}


}


$p1=new Person();


$p1->name="this is a person name";


//在使用isset()函数测定私有成员时,自动调用__isset()方法帮我们完成,返回结果为true


echo var_dump(isset($p1->name))."
";


echo $p1->name."
";


//在使用unset()函数删除私有成员时,自动调用__unset()方法帮我们完成,删除name私有属性


unset($p1->name);


//已经被删除了, 所这行不会有输出


echo $p1->name;


?>


private function __unset($nm)


{

unset($this->$nm);

} Let’s look at a complete example: class Person{
//The following are the member attributes of the person

private $name; //The person’s name
<🎜><🎜>private $sex; 🎜><🎜><🎜>private $age; //Age of a person<🎜><🎜><🎜>//__get() method is used to get 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 propertiesprivate function __set($property_name, $value) { $this->$property_name = $value;}//__isset() methodprivate function __isset($nm){echo "isset( ) function automatically calls
";return isset($this->$nm);}//__unset() methodprivate function __unset($nm){echo "When in
";unset($this->$nm); is automatically called when the unset() function is used outside the class to delete private members. }}$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 trueecho 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 lineecho $p1->name;?> ; The output result is: When the isset() function determines a private member, it automatically calls bool(true) this is a person nameWhen the unset() function is used outside the class to delete private members, it is automatically called__set(), __get(), __isset(), and __unset(). These four methods are all added to the object. Automatically called when needed to complete operations on private properties inside the object outside the object http://www.bkjia.com/PHPjc/444688.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444688.htmlTechArticleWe often see the usage of __set __get __isset __unset in the object-oriented php, but it is very I don’t understand why these things are used, let’s introduce them one by one...
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
Previous article:PHP perfect RSS generation class_PHP tutorialNext article:PHP perfect RSS generation class_PHP tutorial

Related articles

See more