When calling or setting a method that does not exist for a class, _ invokes the _set and __get methods
The following is an example
<?phpclass handsonboy{ Private $name = ' Chenqionghe '; Private $age =; Public Function __set ($name, $value) { echo ' You are setting private properties '. $name. ' <br > value for '. $value. ' <br> '; $this $name = $value; } Public Function __get ($name) { if (!isset ($this, $name)) { echo ' is not set '. $name; $this, $name = "Setting default values for you". <br> '; } return $this $name; }} $a = new Handsonboy (); Echo $a->name. ' <br/> '; $a->hair = ' short ';
PHP overloads differ from languages such as Java. In Java, overloading refers to a class where you can define multiple methods that have different parameter lists but have the same name. For example, Java also has a constructor, as long as the method signature is not the same as the line; PHP allows only one constructor in a class. PHP provides overloads that dynamically create class properties and methods. Therefore, the __set and __get methods are grouped into overloads.
__set and __get Methods in PHP