Detailed explanation of usage examples of __get() and __set() in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:09:10
Original
929 people have browsed it

Usage of php object-oriented _get(), _set()
Generally speaking, always define the attributes of a class as private, which is more in line with realistic logic. However, the reading and assignment operations on attributes are very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain and assign their attributes. It is similar to the operation of javabean in java, and the method used is similar, except that there is no need to perform set and get operations on each field like in javabean. Just add two magic methods. That is, the operations of setting and obtaining values ​​of private members. PHP5 provides us with special methods for setting and obtaining values ​​for attributes, the two methods "__set()" and "__get()". These two methods do not exist by default, but we add them to the class manually. Inside, like the constructor method (__construct()), it will only exist if it is added inside the class. You can add these two methods in the following way, and of course you can also add them according to your personal style:

Copy code The code is as follows:

//__set() method is used to set private properties
public function __set($name,$value) {
$this->$name = $value;
}
//__get() method is used to obtain private properties
public function __get($name){
return $this ->$name;
}

__get() method: This method is used to get the value of a private member attribute. It has one parameter. The parameter is passed in the name of the member attribute you want to get. , returns the obtained attribute value. We do not need to call this method manually, because we can also make this method a private method, which is automatically called by the object when the private attribute is directly obtained. Because the private property has been encapsulated, the value cannot be obtained directly. However, if you add this method to the class, you will automatically obtain the value directly when using a statement such as "echo$p1->name". Call the __get($name) method and pass the attribute name to the parameter $name. Through the internal execution of this method, the value of the private attribute we passed in is returned. If the member properties 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. It has two parameters. The first parameter is the name of the attribute you want to set the value for, and the second parameter is the attribute you want to set. Set value, no return value. This method also does not need to be called manually. It can also be made private. It is automatically called when directly setting the private attribute value. The same private attribute has been encapsulated with
. If there is no __set() Methods are not allowed, for example: $this->name='zhangsan', this will cause an error, but if you add the __set($property_name, $value) method to the class, you can directly set the private property When assigning a value, it will be automatically called, passing the attribute such as name to $property_name, and passing the value "zhangsan" to be assigned to $value. Through the execution of this method, the purpose of assignment is achieved. If the member properties are not encapsulated as private, the object itself will 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 code The code is as follows:

class Person
{
//The following are the member attributes of a person, which are all encapsulated private members
private $name; //The person’s name
private $sex; //The person’s gender
private $age; // Person's age
//__get() method is used to obtain private properties
private function __get($property_name)
{
echo "When directly obtaining the private property value, this is automatically called_ _get() method
";
if(isset($this->$property_name))
{
return($this->$property_name);
}
else
{
return(NULL);
}
}
//__set() method is used to set private properties
private function __set($property_name, $value)
{
echo "When directly setting the value of a private property, this __set() method is automatically called to assign a value to the private property
";
$this->$property_name = $value;
}
}
$p1=newPerson();
//When directly assigning a value to a private attribute, the __set() method will be automatically called for assignment
$p1->name ="Zhang San";
$p1->sex="Male";
$p1->age=20;
//Get the value of the private attribute directly, and __get( will be automatically called ) method, returns the value of the member attribute
echo "Name: ".$p1->name."
";
echo "Gender: ".$p1->sex."< br>";
echo "Age:".$p1->age."
";
?>

Program execution result:
When directly setting the private attribute value, the __set() method is automatically called to assign the private attribute value
When directly setting the private attribute value, This __set() method is automatically called to assign values ​​to private attributes
When directly setting the value of a private attribute, this __set() method is automatically called to assign a value to a private attribute
When directly obtaining the value of a private attribute, This __get() method is automatically called
Name: Zhang San
When directly obtaining the private attribute value, this __get() method is automatically called
Gender: Male
When directly obtaining the private attribute value When setting the attribute value, the __get() method is automatically called
Age: 20
If the above code does not add the __get() and __set() methods, the program will go wrong, because it cannot be used in the class Externally operate private members, and the above code helps us directly access encapsulated private members by automatically calling the __get() and __set() methods.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327357.htmlTechArticlephp object-oriented usage of _get(), _set() Generally speaking, always define the attributes of the class For private, this is more in line with realistic logic. However, reading and assigning operations to attributes are very frequent...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template