1. When _ get ($ property) accesses an undefined property, it calls classlanjie {function _ get ($ name) {echo $ name. propertynotfound !;}} $ Obnewlanjie (); echo $ ob-g; when we call the $ ob undefined attribute g, call the interceptor _ get () method and output "gpropertynotfound !";
1. When _ get ($ property) accesses an undefined property, class lanjie {function _ get ($ name) {echo $ name. property not found is called! ;}}$ Ob = new lanjie (); echo $ ob-g; when we call the $ ob undefined attribute g, we call the interceptor _ get () method, output "g property not found! ";
1. It is called when _ get ($ property) accesses an undefined property.
class lanjie{ function __get($name) { echo $name." property not found! "; }}$ob = new lanjie();echo $ob->g;
When we call the $ ob undefined attribute g of the object, call the interceptor _ get () method and output "g property not found !";
2. _ set ($ property, $ value) assigns a value to an undefined property call.
class person{ private $_age; private $_name; function __set($name, $value) { $method = "set". ucfirst($name); echo $method; if(method_exists($this, $method) ) { return $this->$method( $value ); } } function setName( $name ) { $this->_name = $name; if( !is_null($this->_name) ) { $this->_name = strtoupper($this->_name); } } function setAge( $age ) { return $this->_age = (int)$age; }}$p = new person();$p->name = 'bob';print_r( array( $p ) );
We can clearly see that when an undefined 'name' value is assigned, "_ set ()" is called ()"
Others include _ call (), _ isset (), and _ unset ();
The most useful and commonly used here is _ call (), which is called when an existing method is called; _ isset () is called when an isset () function is used for a defined attribute, and _ unset is called when unset is used for unset numbers.
Http://blog.csdn.net/shuiping567541/article/details/7061258