Examples of usages of __get and __set in PHP

Source: Internet
Author: User
PHP Object-oriented _get (), _set () usage
In general, it is more realistic logic to always define the properties of a class as private. However, read and assign operations to properties are very frequent, so in PHP5, two functions "__get ()" and "__set ()" are predefined to get and assign their properties. Similar to JavaBean in Java, the methods used are similar, except that you do not need to do set and get operations on each field as in JavaBean. You only need to add two magic methods. That is, the setting of the private member and the operation of the value. In PHP5, we are provided with a method for setting values and getting values specifically for attributes, "__set ()" and "__get ()", two methods that are not by default, but that we add to the class by hand, like the construction Method (__construct ()). Class is added to exist, you can add these two methods in the following way, of course, you can also add in personal style:

Copy the Code code as follows:


The __set () method is used to set the private property
Public Function __set ($name, $value) {
$this $name = $value;
}
The __get () method is used to get the private property
Public Function __get ($name) {
return $this $name;
}


__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, but if you add this method to the class, the __get ($name) method is automatically called when a statement using "Echo$p1->name" is obtained directly, and the property name is passed to the parameter $ Name, through the internal execution of this method, returns the value of the private property that we passed in. 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 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 invoked, the same property private has been encapsulated
, if there is no __set () This method is not allowed, such as: $this->name= ' Zhangsan ', this will be wrong, but if you add __set in the Class ($property _name, $value) This method, When assigning a value directly to a private property, it is automatically invoked, passing the attribute, such as name, to the $property_name, passing the value "Zhangsan" to the $value, which is executed to achieve the purpose of the 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:

Copy the Code code as follows:


Class Person
{
The following are the member properties of a person, both encapsulated private members
Private $name; The name of the man
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=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 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 value to a private property
The __set () method is automatically called when a private property value is set directly to assign a value to a 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:

If the above code does not add the __get () and __set () methods, the program will go wrong because the private members cannot be manipulated outside the class, and the above code is automatically called by the __get () and __set () methods to help us directly access the encapsulated private members.

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

The above describes the PHP __get and __set usage examples, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

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