Home  >  Article  >  Backend Development  >  How to use _get(),_set() in php

How to use _get(),_set() in php

不言
不言Original
2018-07-20 16:36:471565browse

PHP object-oriented _get(), _set() how to use? The __set() method sets the value for the attribute, and the __get() method obtains the value for the attribute. Both of these methods need to be added to the program manually. Next, let’s take a closer look at the use of these two methods. .

Generally speaking, always define the attributes of a class as private, which is more in line with realistic logic. However, reading and assigning operations to attributes are very frequent, so in PHP5, two functions "__get()" and "__set()" are predefined to obtain and assign 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. Of course, you can also add them according to your personal style:

1 //__set()方法用来设置私有属性 
2 public function __set($name,$value){ 
3 $this->$name = $value; 
4 } 
5 //__get()方法用来获取私有属性 
6 public function __get($name){ 
7 return $this->$name; 
8 }

__get() method: This method is used to get the private member attribute value. It has a parameter. The parameter is passed in the name of the member attribute you want to get, and the obtained attribute value is returned. This method does not need to be called manually, because we This method can also be made into a private method, which is automatically called by the object when private properties are 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
. 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:

 1 <?php 
 2 class Person 
 3 { 
 4 //下面是人的成员属性,都是封装的私有成员 
 5 private $name; //人的名子 
 6 private $sex; //人的性别 
 7 private $age; //人的年龄 
 8 //__get()方法用来获取私有属性 
 9 private function __get($property_name) 
10 { 
11 echo "在直接获取私有属性值的时候,自动调用了这个__get()方法<br>"; 
12 if(isset($this->$property_name)) 
13 { 
14 return($this->$property_name); 
15 } 
16 else 
17 { 
18 return(NULL); 
19 } 
20 } 
21 //__set()方法用来设置私有属性 
22 private function __set($property_name, $value) 
23 { 
24 echo "在直接设置私有属性值的时候,自动调用了这个__set()方法为私有属性赋值<br>"; 
25 $this->$property_name = $value; 
26 } 
27 } 
28 $p1=newPerson(); 
29 //直接为私有属性赋值的操作,会自动调用__set()方法进行赋值 
30 $p1->name="张三"; 
31 $p1->sex="男"; 
32 $p1->age=20; 
33 //直接获取私有属性的值,会自动调用__get()方法,返回成员属性的值 
34 echo "姓名:".$p1->name."<br>"; 
35 echo "性别:".$p1->sex."<br>"; 
36 echo "年龄:".$p1->age."<br>"; 
37 ?>

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 , the __set() method is automatically called to assign values ​​to private attributes
When directly setting the value of a private attribute, the __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 adding a private 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 External operation of private members, and the above code helps us directly access the encapsulated private members by automatically calling the __get() and __set() methods.

Related recommendations:

About PHP’s _set() and _get()

The above is the detailed content of How to use _get(),_set() in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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