In the class, the general set and get methods are designed to simplify the reading and writing of properties, which differs from the normal get and set methods for individual properties, which must provide a pair of methods for each property, and therefore, Can be thought of as a strategy for bulk defining set and get methods.
Another common magic method is construct, which is described here as a control.
First, about the access permission modifier. Construct is designed to be public, the system calls the construction method to initialize the object when new is created, otherwise it cannot be instantiated (usually in a pure static class, or singleton mode), and set and get, which are designed as private, do not affect the function itself. That is, the system calls these methods are not affected by access rights. and are designed to be public, you can call these methods themselves directly.
Second, about the return value. The return value of the Get method, as the result of the property, and the return value of the construct and set methods is meaningless and will not be adopted. As follows:
echo $obj->pro = "value";
The value to be printed will be values, regardless of the return value of the Set method.
Third, about the use of the situation. If you need to get the $obj->pro value, you will first look for the public pro property, if not found, will see if there is a Get method, and return the value as a result, if there is no get method, will look for a private property, found after the error, if the private property can not be found, A public property is created temporarily.
<? PHP class user{ private$id; Private $name ;} $user New User (); $user->ac = 6;//Do not error, temporarily generate a public property Echo$user-ac; >
In the case of $obj->pro = "value", the public pro property is first searched, and if not found, the Set method is enabled, like get.
The above syntax is also useful in the class itself, if a method of the class has an $obj->pro expression, then it looks for the property first, then takes the Get method, and of course, both the private and public properties are searched first, and then the Get method is considered, and the set method is similar.
But the Isset magic method, which takes precedence over the Get method, in other words, if the client code is as follows:
Isset ($obj->pro), first checks if there is a pro public variable, then checks the Isset method, if any, will read the Isset method, and then determine the return value as the check result, if not, will consider the process of using the Get method.
The forth is about inheritance. These magic methods are inherited by the quilt class, and when inherited, the parent class can access non-private variables and methods in the subclass, but cannot access private variables and methods. This problem leads to a noticeable detail, such as in the parent class, where we define the set method to be assigned to a variable called _pro, and if the subclass inherits the set method directly, it will be able to assign a valid value to the private variable defined in the parent class, but for the private variable in the subclass, It does not have polymorphic permissions and, therefore, cannot be assigned a value.
classuser{Private $id; Private $name= "Jiangbo"; Public function__get ($property _name) { if(isset($this-$property _name)) { if($property _name= = "CD"){ return $this-_cdtime; } return($this-$property _name); } Else { return("No Find"); } } //The __set () method is used to set the private property Public function__set ($property _name,$value) { $this-$property _name=$value; }}$user=NewUser ();Echo $user->ac;//"No Find"Echo"<br>";Echo $user->name;
PHP Get Set method in-depth understanding