PHP Object-oriented (vi) __set () __get () __isset () __unset () Usage _php tutorial

Source: Internet
Author: User
application of four methods of 10.__set () __get () __isset () __unset ()
In general, it is more realistic logic to always define the properties of a class as private. However, the read of the property
and assignment operations are very frequent, so in PHP5, two functions "__get ()" and "__set ()" are predefined to obtain
Take and assign its properties, and examine the property's "__isset ()" and the Method "__unset ()" To delete the property.
In the previous section, we set up and obtained a method for each property, and in PHP5 we provided a special
The methods of setting values and getting values, "__set ()" and "__get ()" are two methods that do not exist by default,
Instead, we add it to the class by hand, like the construction Method (__construct ()), where the class is added to exist,
You can add these two methods in the following way, but you can also add them as a personal style:
Code Snippets
Copy CodeThe code is as follows:
The __get () method is used to get the private property
Private 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 property
Private 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 is passed to you to get the
The name of the member property, returns the property value obtained, this method does not need to be called by us manually, because we can also put
This method is made private, and is automatically invoked when the object is taken directly from the private property. Because the private property is
After being encapsulated, it is not possible to get the value directly (for example: "Echo $p 1->name"), the direct acquisition is wrong,
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
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 the one you want to
To set the property name of a value, the second parameter is the value to set for the property, and no return value. This method also does not need us
By hand, it can also be made private, which is automatically called when the value of the private property is set directly, and the same property
Private has been encapsulated, if not __set () This method is not allowed, such as:
$this->name= ' Zhangsan ', this can go wrong, but if you add __set to the class ($property _name, $value)
This method, when assigning a value directly to a private property, automatically calls it, passing attributes such as name to
$property _name, the value to be assigned "Zhangsan" passed to $value, through the execution of this method, to achieve the assigned value of the target
Of 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
Value, you can also make a judgment in this method. The code is as follows: Code snippet
Copy CodeThe code is as follows:
Class person{
The following are the member properties of a person, both encapsulated private members
Private $name; Man's name
Private $sex; Man's Sex
Private $age; The age of the person
The __get () method is used to get the private property
Private Function __get ($property _name) {
echo "Automatically calls this __get () method when it directly acquires the value of a private property.
";
if (Isset ($this-$property _name)) {
Return ($this, $property _name);
}else {
return (NULL);
}
}
The __set () method is used to set the private property
Private Function __set ($property _name, $value) {
echo "Automatically calls this __set () method to assign a value to a private property when setting the value of a private property directly
";
$this $property _name = $value;
}
}
$p 1=new person ();
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 property
echo "Name:". $p 1->name. "
";
echo "Gender:". $p 1->sex. "
";
echo "Age:" $p 1->age. "
";
?>

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.
The __set () method is automatically called when a private property value is set directly to assign a value to a 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 we 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 if you use the "isset ()" function outside of an object to determine if the members of the object are set, can
With it? In two cases, if the members of the object are public, we can use this function to determine the member
property, if it is a private member attribute, this function does not work, because the private is encapsulated, outside
Part is not visible. Then we're not going to
Use the "isset ()" function outside the object to determine if the private member property is set? Yes, you just
Add a "__isset ()" method to the class and use the "isset ()" function outside the class to measure the object
If a private member of a polygon is set, the "__isset ()" method in the class is called automatically to help us do this
, the "__isset ()" method can also be made private. You can add the following code to the class:
Code Snippets
Copy CodeThe code is as follows:
Private Function __isset ($NM) {
echo "When you use the Isset () function outside a class to measure a private member $nm, it is automatically called
";
Return Isset ($this-$NM);
}

__unset () Method: Before we look at this method, let's take a look at the "unset ()" function, "unset ()"
The function is to delete the specified variable and return True, and the argument is the variable to be deleted. So if you're in an object
Outside to remove the member property inside the object with the "unset ()" function, it is also divided into two cases, if a
The member property inside the object is public, you can use this function to delete the public property of the object outside the object, if
The member property of the object is private, and I do not have permission to delete it with this function, but also if you are in an object
With the "__unset ()" method, you can delete the object's private member properties outside the object. In the object
After adding the "__unset ()" method, use the "unset ()" function outside the object to remove the private inside of the object
member property, the "__unset ()" function is called automatically to help us remove the private member property inside the object, which can also be defined as private within the class. Add the following code to the object:
Code Snippets
Copy CodeThe code is as follows:
Private Function __unset ($NM) {
echo "When you use the unset () function outside of a class to delete a private member, the automatically called
";
Unset ($this, $nm);
}

Let's look at a complete example:
Code Snippets
Copy CodeThe code is as follows:
Class person{
The following is the person's member property
Private $name; Man's name
Private $sex; Man's Sex
Private $age; The age of the person
The __get () method is used to get the private property
Private 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 property
Private Function __set ($property _name, $value) {
$this $property _name = $value;
}
__isset () method
Private Function __isset ($NM) {
The echo "Isset () function automatically calls when a private member is measured
";
Return Isset ($this-$NM);
}
__unset () method
Private Function __unset ($NM) {
echo "When you use the unset () function outside of a class to delete a private member, the automatically called
";
Unset ($this, $nm);
}
}
$p 1=new person ();
$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 True
Echo Var_dump (Isset ($p 1->name)). "
";
echo $p 1->name. "
";
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 property
unset ($p 1->name);
has been removed, there is no output for this line
Echo $p 1->name;
?>

The output is:
The Isset () function automatically calls when a private member is measured
BOOL (TRUE)
This was a person name
Automatically called when a private member is removed by using the unset () function outside the class.
__set (), __get (), __isset (), __unset () are all four methods we have added to the object,
Automatically called to complete the operation of the object's internal private property outside the object.

http://www.bkjia.com/PHPjc/320645.html www.bkjia.com true http://www.bkjia.com/PHPjc/320645.html techarticle 10.__set () __get () __isset () __unset () The application of four methods in general, it is more realistic to define the properties of a class as private. However, read and assign the property ...

  • 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.