Php object-oriented _ get (), set () usage

Source: Internet
Author: User
[Conversion] php object-oriented _ get (), _ set () usage generally defines the class attribute as private, which is more in line with the actual logic. However, attributes are read and assigned frequently. Therefore, in PHP5, we predefine the two functions "_ get ()" and "_ set () to get and assign values to the usage of [convert] php object-oriented _ get (), _ set ()

In general, the class attribute is always defined as private, which is more in line with the actual logic. However, attributes are read and assigned frequently. Therefore, in PHP5, we predefine the two functions "_ get ()" and "_ set () to obtain and assign values to the attributes, and check the "_ isset ()" and the method "_ unset ()" for deleting the attributes ()". In the previous section, we set and obtain each attribute. in PHP5, we provided a method specifically for setting and obtaining values for the attribute, "_ set () "and" _ get () "methods. These two methods do not exist by default, but are manually added to the class, like the constructor (_ construct ,? The two methods can be added only when they are added in the class. you can add them in the following way. of course, you can also add them in your own style: // _ get () to obtain the private function? _ Get ($ property_name) {if (isset ($ this-> $ property_name) {return ($ this-> $ property_name );} the else {return (NULL) ;}// _ set () method is used to set the private attribute private function? _ Set ($ property_name ,? $ Value) {$ this-> $ property_name? =? $ Value ;:__ get () method: This method is used to obtain the attribute value of a private member. a parameter is used to input the name of the member attribute you want to obtain and return the obtained attribute value, this method does not need to be manually called, because we can also make this method a private method, which is automatically called when the object directly obtains the private property. Because private attributes have been encapsulated, values cannot be obtained directly (for example, "echo $ p1-> name" is incorrect ), however, if you add this method to the class, when you use a statement such as "echo $ p1-> name" to directly obtain the value, it will automatically call _ get ($ property_name) to pass the attribute name to the parameter $ property_name. The value of the private attribute we passed in is returned through the internal execution of this method. If the member attributes 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. There are two parameters. The first parameter is the attribute name for the set value, the second parameter is the value to be set for the attribute, with no return value. This method does not need to be called manually. It can also be made private. it is automatically called when private property values are directly set. Similarly, private attributes are encapsulated, the _ set () method is not allowed, for example, $ this-> name = 'hangsan ',? This will cause an error, but if you add the _ set ($ property_name, $ value) method to the class, it will be automatically called when the private property is assigned a value directly, pass attributes such as name to $ property_name ,? Pass the value "zhangsan" to $ value. this method is used for value assignment. If the member attributes are not encapsulated as private, the object itself will not automatically call this method. In order not to pass in invalid values, you can also make a judgment in this method. The code is as follows: "; If (isset ($ this-> $ property_name) {return ($ this-> $ property_name);} else {return (NULL );}} // _ set () is used to set the private property private function? _ Set ($ property_name ,? $ Value) {echo? "When setting the private property value directly, the _ set () method is automatically called to assign values to the private property.
"; $ This-> $ property_name? =? $ Value ;}$ p1 = new? Person (); // operations that directly assign values to private attributes will automatically call the _ set () method to assign values $ p1-> name = "James "; $ p1-> sex = "male"; $ p1-> age = 20; // The _ get () method is automatically called to directly obtain the value of a private attribute, returns the member attribute value echo? "Name:". $ p1-> name ."
"; Echo? "Gender:". $ p1-> sex ."
"; Echo? "Age:". $ p1-> age ."
";?> 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 setting the private property value directly, the _ set () method is automatically called to assign values to the 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: if the code above 20 is not added with the _ get () and _ set () methods, the program will fail because private members cannot be operated outside the class, the above code automatically calls the _ get () and _ set () methods to help us directly access the encapsulated private members.

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.