Usage examples of _ get and _ set in PHP

Source: Internet
Author: User
: This article describes the usage examples of _ get and _ set in PHP. For more information about PHP tutorials, see. Php object-oriented _ get (), _ set () usage
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 attributes. Similar to operations on javabean in java, the method used is similar, but you do not need to perform the set and get operations on each field as in javabean. You only need to add two magic methods. That is, the operation of setting values and values for private members. In PHP5, we provided two methods for setting values for attributes and getting values: "_ set ()" and "_ get, these two methods do not exist by default, but are manually added to the class. like the constructor (_ construct (), they exist only when they are added to the class, you can add these two methods in the following way, or you can add them in your own style:

The code is as follows:


// _ Set () is used to set private attributes.
Public function _ set ($ name, $ value ){
$ This-> $ name = $ value;
}
// _ Get () is used to obtain private attributes.
Public function _ get ($ name ){
Return $ this-> $ name;
}


_ Get (): This method is used to obtain the attribute value of a private member. there is a parameter that 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, but if you add this method to the class, when you use a statement such as "echo $ p1-> name" to directly obtain the value, the _ get ($ name) method is automatically called to pass the attribute name to the parameter $ name, the internal execution of this method returns the value of the private attribute we passed in. 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, the private property has been encapsulated.
The _ set () method is not allowed, for example, $ this-> name = 'hangsan, however, 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 the attribute such as name to $ property_name, and pass the value "zhangsan" to $ value. this method is used to assign values. 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:

The code is as follows:


Class Person
{
// The following are the member attributes of a person, all of which are encapsulated private members.
Private $ name; // 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.
";
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 the private property value is directly set, the _ set () method is automatically called to assign values to the private property.
";
$ This-> $ property_name = $ value;
}
}
$ P1 = newPerson ();
// 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 ."
";
Echo "gender:". $ p1-> sex ."
";
Echo "age:". $ p1-> age ."
";
?>


Program execution result:
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.
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 does not contain the _ get () and _ set () methods, the program will fail because private members cannot be operated on outside the class, the above code automatically calls the _ get () and _ set () methods to help us directly access the encapsulated private members.

This article reprinted from: http://www.jb51.net/article/37900.htm

The above describes the usage examples of _ get and _ set in PHP, including some content, and hope to help those who are interested in PHP tutorials.

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.