Home >Backend Development >PHP Tutorial >Examples of usage of __get() and __set magic methods in PHP
__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 can also use this method. The private method is automatically called by the object when the private property 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, it will be automatically called when you use a statement such as "echo$p1->name" to directly obtain the value. The __get($name) method passes 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 value you want to set for the attribute. , 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() method, it is Not allowed, for example: $this->name='zhangsan', this will cause an error. If the __set($property_name, $value) method is added to the class, when directly assigning a value to the private property, it will Automatically call it, pass attributes such as name to $property_name, and pass 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. For example:
Program execution results: When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute. When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute. When directly setting the value of a private attribute, the __set() method is automatically called to assign a value to the private attribute. When directly obtaining the private attribute value, the __get() method is automatically called. Name: Zhang San When directly obtaining the private attribute value, the __get() method is automatically called. Sex: Male When directly obtaining the private attribute value, the __get() method is automatically called. Age: 20 In the above code, if the __get() and __set() methods are not added, the program will go wrong, because private members cannot be operated outside the class, and the above code automatically calls __get() and __set() Method to directly access the encapsulated private members. That’s it. I hope it will help everyone understand the usage of __get() and __set(). |