PHP __set () __get () __isset () __unset () Four ways to apply

Source: Internet
Author: User

Application of four methods of _set () __get () __isset () __unset ()

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:

Code Snippets

//The __get () method is used to get the private propertyPrivate 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 propertyPrivate 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, you get the value directly using the "Echo $p 1->name" statement. The __get ($property _name) method is automatically invoked, passing the property name to the parameter $property_name, which returns the value of the private property passed in through the internal execution of the method. If the member property is not encapsulated as private, the object

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 called, the same property private has been encapsulated, if there is no __set () This method is not allowed, such as: $this->name= ' Zhangsan ' This will make a mistake, but if you add __set ($property _name, $value) to the class, it will automatically be called when you assign a value directly to the private property, passing the attribute such as name to $property _name, and assigning the value " Zhangsan "passed to $value, through the execution of this method, to achieve the purpose of 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: Code snippet \

The code is as follows:

<?PHPclassperson{//The following are the member properties of a person, both encapsulated private membersPrivate $name;//Man's namePrivate $sex;//Man's SexPrivate $age;//man's Age//__get () method to get a private propertyPrivate function__get ($property _name){ Echo"This __get () method is automatically called when the private property value is obtained directly <br>"; if(isset($this-$property _name)) { return($this-$property _name); }Else { return(NULL); } } //The __set () method is used to set the private propertyPrivate function__set ($property _name,$value){ Echo"When setting private property values directly, this __set () method is called automatically to assign a value to a private property <br>"; $this-$property _name=$value; } } $p 1=NewPerson ();//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 propertyEcho"Name:".$p 1->name. " <br> "; Echo"Gender:".$p 1->sex. " <br> "; Echo"Age:".$p 1->age. " <br> "; ?>


Program execution Results:
The __set () method is automatically called when a private property value is set directly to assign a private property value when the private property is set directly, and the __set () method is called automatically to assign a value to the private property.
This __get () method is called automatically when a private property value is obtained directly
Name: Zhang San
This __get () method is called automatically when a private property value is obtained directly
Gender: Male
This __get () method is called automatically when a private property value is obtained directly
Age: 20
If the above code does not add the __get () and the __set () method, the program will go wrong because it cannot operate on the outside of the class
There are members, and the above code is automatically called by the __get () and the __set () method to help us directly access the encapsulated private
Members of the.


__isset () Method:

Before looking at this method, let's take a 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:

Code Snippets

The code is as follows:


Private function __isset ($nm) {  echo ] automatically calls when a private member $NM is measured outside the class using the Isset () function <br   > ";  returnisset($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 the "__unset ()" method to the object, it automatically calls the "__unset ()" function to remove the private member property inside the object when using the "unset ()" function outside the object to delete the property of the private members inside the object. This method can also be defined as private within the class. Add the following code to the object:

Code Snippets

The code is as follows:

Private   function __unset ($nm) {  echo ] automatically invokes <br> "when using the unset () function outside of a class to delete a private member;  unset($this,$nm);}

Let's look at a complete example:

The code is as follows:


<?PHPclassperson{//The following is the person's member propertyPrivate $name;//Man's namePrivate $sex;//Man's SexPrivate $age;//man's Age//__get () method to get a private propertyPrivate 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 propertyPrivate function__set ($property _name,$value) {$this-$property _name=$value; } //__isset () methodPrivate function__isset ($nm) {EchoThe "isset () function automatically invokes the <br>" when measuring a private member; return isset($this-$nm); } //__unset () method   Private function__unset ($nm) {Echo"When using the unset () function outside of a class to delete a private member automatically called <br>"; unset($this-$nm); } } $p 1=NewPerson ();$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 TrueEcho Var_dump(isset($p 1->name)). " <br> "; Echo $p 1->name. " <br> "; //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 propertyunset($p 1-name); //has been removed, there is no output for this lineEcho $p 1-name;?>

The output is:
The Isset () function automatically calls when a private member is measured
BOOL (TRUE)
This was a person name
__set (), __get (), __isset (), __unset (), which are automatically called when the unset () function is used outside the class to remove a private member, are the four methods we add to the object, which are called automatically when needed. To complete the operation of the object's internal private property outside the object.

PHP __set () __get () __isset () __unset () Four ways to apply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.