PHP provides several interceptors that are called when accessing undefined methods and properties, and __get ($property) is called when an undefined class property is accessed, $property the name of the property being accessed. When an undefined class property is assigned a value, __set ($property, $value) is called, $property is the assigned property name, $value the assigned value.
First download the various interceptor libraries in PHP that we need to use for this lesson: http://www.php.cn/xiazai/leiku/625
After the download, find the PHP class files we need, unzip to our local directory, create a new PHP file!
Once this is done, we will invoke this class in the new PHP file, and instantiate the class:
<?phpinclude_once "dingyi.php";//Introduce class file $ob = new Lanjie (); Instantiate//When we call the object $ob undefined property g, call the Interceptor __get () method, Output "G Property not found!" echo $ob->g;echo "<br>"; $p = new Person (); Instantiate $p->name = ' bob ';//Here we can clearly see that when assigning a value to an undefined ' name ', the "__set ()" Print_r (Array ($p)) is called; >
Run this file and get the results as shown: