Php object-oriented full strategy (6) _ set () _ get () _ isset () _ unset () Usage

Source: Internet
Author: User

10. _ set () _ get () _ isset () _ unset () application of four methods
In general, the class attribute is always defined as private, which is more in line with the actual logic. However, read the attribute
And value assignment operations are very frequent. Therefore, in PHP5, we predefine the two functions "_ get ()" and "_ set ()" to obtain
Take and assign values to its attributes, and check the attribute "_ isset ()" and the method "_ unset ()" for deleting the attribute ()".
In the previous section, we set and obtain each attribute. In PHP5, we provide a dedicated
Sets the value and the method to obtain the value. The "_ set ()" and "_ get ()" methods do not exist by default,
Instead, we manually add it to the class. Like the constructor (_ construct (), it will only exist if it is added to the class,
You can add these two methods in the following way, or you can add them in your own style:
Code snippet
Copy codeThe Code is as follows:
// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name ){
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name );
} Else {
Return (NULL );
}
// _ Set () is used to set Private attributes.
Private function _ set ($ property_name, $ value ){
$ This-> $ property_name = $ value;
}

_ Get (): This method is used to obtain the attribute values of private members. There is a parameter that is passed in to
This method does not need to be manually called because we can also
This method is a private method, which is automatically called by the object when the private property is directly obtained. Because the private property already exists
After being encapsulated, you cannot directly obtain the value (for example, "echo $ p1-> name ),
However, if you add this method to the class, you can directly obtain the value using a statement such as "echo $ p1-> name ".
The _ get ($ property_name) method will be automatically called to pass the attribute name to the parameter $ property_name. Through the internal execution of this method, the value of the private attribute we passed in will be returned. If the member attributes are 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 required for you.
For the attribute name of the set value, the second parameter is the value to be set for the attribute, no return value. This method also does not need us
It can also be called manually. It is automatically called when private property values are directly set.
Private has been encapsulated. If there is no _ set () method, it is not allowed. For example:
$ This-> name = 'hangsan', this will cause an error, but if you add _ set ($ property_name, $ value) to the class)
This method automatically calls a private attribute when it is assigned a value directly, and passes the attribute such as name
$ Property_name: Pass the value "zhangsan" to $ value. The value is assigned by executing this method.
. If the member attributes are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in illegal
You can also make a judgment in this method. The code snippet is as follows:
Copy codeThe Code is as follows:
<? Php
Class Person {
// The following are the member attributes of a person, all of which are encapsulated private members.
Private $ name; // the name of a person.
Private $ sex; // gender of a person
Private $ age; // age of a person
// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name ){
Echo "the _ get () method is automatically called when the private property value is directly obtained. <br> ";
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name );
} Else {
Return (NULL );
}
}
// _ Set () is used to set Private attributes.
Private function _ set ($ property_name, $ value ){
Echo "when setting private property values directly, the _ set () method is automatically called to assign values to private properties. <br> ";
$ This-> $ property_name = $ value;
}
}
$ P1 = new Person ();
// Operations that directly assign values to private properties automatically call the _ set () method to assign values.
$ P1-> name = "James ";
$ P1-> sex = "male ";
$ P1-> age = 20;
// Directly obtain the value of the private attribute. The _ get () method is 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 result:
When the private property value is directly set, the _ set () method is automatically called to assign values to the private property. When the private property value is directly set, the _ set () method is automatically called () the method is to assign a value to a private property.
When setting the private property value directly, the _ set () method is automatically called to assign values to the private property.
The _ get () method is automatically called when the private property value is obtained directly.
Name: James
The _ get () method is automatically called when the private property value is obtained directly.
Gender: male
The _ get () method is automatically called when the private property value is obtained directly.
Age: 20
If the above Code is not added with the _ get () and _ set () methods, the program will encounter an error because it cannot perform private operations outside the class.
The above code automatically calls the _ get () and _ set () methods to help us directly access the encapsulated private
Member.
_ Isset () method: Before reading this method, let's take a look at the application of the "isset ()" function. isset () is a function used to determine whether variables are set, if a variable exists, true is returned. Otherwise, false is returned.
If you use the "isset ()" function outside an object to determine whether the Members in the object are set, can you?
What about it? In two cases, if the Members in the object are public, we can use this function to determine the Member owner.
If it is a private member attribute, this function does not work because the private member is encapsulated.
Is invisible. Then we cannot
Use the "isset ()" function outside the object to determine whether the private member attribute is set? Yes, you only need
Add the "_ isset ()" method to the class. When the class is external, use the "isset ()" function to determine the object.
When the private member is set, the "_ isset ()" method in the class is automatically called to help us complete this operation.
The "_ isset ()" method can also be made private. You can add the following code to the class:
Code snippet
Copy codeThe Code is as follows:
Private function _ isset ($ nm ){
Echo "automatically called when the isset () function is used outside the class to determine the private member $ nm <br> ";
Return isset ($ this-> $ nm );
}

_ Unset () method: Before reading this method, let's take a look at the function "unset ()" and "unset ()"
This function is used to delete the specified variable and return true. The parameter is the variable to be deleted. Then, if an object
Can I use the "unset ()" function to delete the member attributes inside an object externally? In either case
If the member attribute in the object is public, you can use this function to delete the public attribute of the object outside the object.
The member attributes of an object are private. I do not have the permission to delete the object when using this function.
Add the "_ unset ()" method to the interface to delete the private member attribute of the object outside the object. In the object
After the "_ unset ()" method is added, the "unset ()" function is used outside the object to delete the private
When a member attribute is set, the "_ unset ()" function is automatically called to delete the private member attribute inside the object. This method can also be defined as private within the class. Add the following code to the object:
Code snippet
Copy codeThe Code is as follows:
Private function _ unset ($ nm ){
Echo "automatically called when the unset () function is used outside the class to delete Private Members <br> ";
Unset ($ this-> $ nm );
}

Let's look at a complete example:
Code snippet
Copy codeThe Code is as follows:
<? Php
Class Person {
// The following are the member attributes of a person.
Private $ name; // the name of a person.
Private $ sex; // gender of a person
Private $ age; // age of a person
// _ Get () is used to obtain private attributes.
Private function _ get ($ property_name ){
If (isset ($ this-> $ property_name )){
Return ($ this-> $ property_name);} else {
Return (NULL );
}
}
}
// _ Set () is used to set Private attributes.
Private function _ set ($ property_name, $ value ){
$ This-> $ property_name = $ value;
}
// _ Isset () method
Private function _ isset ($ nm ){
Echo "automatically called when the isset () function is used to determine Private Members <br> ";
Return isset ($ this-> $ nm );
}
// _ Unset () method
Private function _ unset ($ nm ){
Echo "automatically called when the unset () function is used outside the class to delete Private Members <br> ";
Unset ($ this-> $ nm );
}
}
$ P1 = new Person ();
$ P1-> name = "this is a person name ";
// When the isset () function is used to determine a private member, the _ isset () method is automatically called to help us complete the measurement. The returned result is true.
Echo var_dump (isset ($ p1-> name). "<br> ";
Echo $ p1-> name. "<br> ";
// When the unset () function is used to delete a private member, the _ unset () method is automatically called to help us complete the deletion.
Unset ($ p1-> name );
// The row has been deleted and no output exists.
Echo $ p1-> name;
?>

Output result:
When the isset () function is used to determine a private member, it is automatically called.
Bool (true)
This is a person name
Automatically called when the unset () function is used outside the class to delete Private Members
The Methods _ set (), _ get (), _ isset (), and _ unset () are all added to the object.
Automatically called to complete the operation on the internal private properties of the object outside the object.

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.