Home  >  Article  >  Backend Development  >  Examples of usage of __get() and __set magic methods in PHP

Examples of usage of __get() and __set magic methods in PHP

WBOY
WBOYOriginal
2016-07-25 08:59:06951browse
  1. //__set() method is used to set private properties
  2. public function __set($name,$value){
  3. $this->$name = $value;
  4. }
  5. / The /__get() method is used to get private properties
  6. public function __get($name){
  7. return $this->$name;
  8. }
  9. ?>
Copy code

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

  1. class Person
  2. {
  3. //Person’s member attributes are all encapsulated private members
  4. private $name; //Person’s name
  5. private $sex; //Person’s gender
  6. private $age; //A person's age
  7. //__get() method is used to obtain private properties
  8. private function __get($property_name)
  9. {
  10. echo "When directly obtaining the private property value, this __get is automatically called ()method
    ";
  11. if(isset($this->$property_name))
  12. {
  13. return($this->$property_name);
  14. }
  15. else
  16. {
  17. return(NULL);
  18. }
  19. }
  20. //__set() method is used to set private properties
  21. private function __set($property_name, $value)
  22. {
  23. echo "When directly setting the private property value, this __set() method is automatically called as Private property assignment
    ";
  24. $this->$property_name = $value;
  25. }
  26. }
  27. $p1=newPerson();
  28. //The operation of directly assigning a value to a private property will automatically call __set( ) method to assign values
  29. $p1->name="Zhang San";
  30. $p1->sex="Male";
  31. $p1->age=20;
  32. //Get the value of the private attribute directly, it will Automatically call the __get() method to return the value of the member attribute
  33. echo "Name:".$p1->name."
    ";
  34. echo "Gender:".$p1->sex."< ;br>";
  35. echo "Age:".$p1->age."
    ";
  36. ?>
Copy code

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().



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