Home  >  Article  >  Backend Development  >  What is the use of __get method in php

What is the use of __get method in php

王林
王林Original
2020-11-04 09:19:594279browse

The role of the __get method in php: used to obtain private member attribute values. The __get method does not need to be called manually, so we can make this method a private method, which is automatically called by the object when obtaining private properties.

What is the use of __get method in php

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

(Recommended tutorial: php video tutorial)

In PHP5, we are provided with special methods for setting and obtaining values ​​for attributes, "__set()" and The two methods "__get()" do not exist by default, but are manually added to the class. Like the constructor method (__construct()), they will only exist if they are added to the class. You can press Add these two methods in the following way. Of course, you can also add them according to your personal style:

//__set() 方法用来设置私有属性
    public function __set($name,$value){
        $this->$name = $value;
    }
    
//__get() 方法用来获取私有属性
    public function __get($name){
        return $this->$name;
    }

__get() method: This method is used to get the private member attribute value. It has one parameter and the parameter is passed in. The name of the member attribute you want to obtain returns the obtained attribute value. We do not need to call this method manually, so 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 can obtain it directly using a statement such as "echo$p1->name" When the value is set, the __get($name) method will be automatically called, and the attribute name will be passed to the parameter $name. Through the internal execution of this method, the value of the private attribute we passed in will be 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 and is automatically called when the private attribute value is directly set.

The same private attributes have been encapsulated. If there is no __set() method, it is not allowed. For example: $this->name= ' zhangsan ', this will cause an error, but if you are The __set($property_name, $value) method is added to the class. When directly assigning a value to a private property, it will be automatically called. Pass the attribute such as name to $property_name, and pass the value to be assigned " zhangsan " $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:

<?php
class  Person
{
// 下面是人的成员属性,都是封装的私有成员
private $ name ;       // 人的名子
private $ sex ;        // 人的性别
private $ age ;        // 人的年龄
//__get() 方法用来获取私有属性
private function  __get( $ property_name )
{
echo  " 在直接获取私有属性值的时候,自动调用了这个 __get() 方法 <br>" ;
if ( isset ( $ this -> $ property_name ))
{
return ( $ this -> $ property_name );
}
else
{
return ( NULL );
}
}
//__set() 方法用来设置私有属性
private function  __set( $ property_name ,  $ value )
{
echo  " 在直接设置私有属性值的时候,自动调用了这个 __set() 方法为私有属性赋值 <br>" ;
$ this -> $ property_name  =  $ value ;
}
}
$ p1 = new Person();
// 直接为私有属性赋值的操作,会自动调用 __set() 方法进行赋值
$ p1 ->name= " 张三 " ;
$ p1 ->sex= " 男 " ;
$ p1 ->age= 20 ;
// 直接获取私有属性的值,会自动调用 __get() 方法,返回成员属性的值
echo  " 姓名: " . $ p1 ->name. "<br>" ;
echo  " 性别: " . $ p1 ->sex. "<br>" ;
echo  " 年龄: " . $ p1 ->age. "<br>" ;
?>

Execution result:

在直接设置私有属性值的时候,自动调用了这个 __set() 方法为私有属性赋值
在直接设置私有属性值的时候,自动调用了这个 __set() 方法为私有属性赋值
在直接设置私有属性值的时候,自动调用了这个 __set() 方法为私有属性赋值
在直接获取私有属性值的时候,自动调用了这个 __get() 方法
姓名:张三
在直接获取私有属性值的时候,自动调用了这个 __get() 方法
性别:男
在直接获取私有属性值的时候,自动调用了这个 __get() 方法
年龄: 20

If the above code does not add the __get() and __set() methods, the program will error because it cannot Private members are operated outside the class, and the above code helps us directly access the encapsulated private members by automatically calling the __get() and __set() methods.

Related recommendations: php training

The above is the detailed content of What is the use of __get method 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