1、__get( $property ) 訪問未定義的屬性時調用
class lanjie{ function __get($name) { echo $name." property not found! "; }}$ob = new lanjie();echo $ob->g;
當我們調用對象$ob未定義的屬性g時,調用攔截器__get()方法,輸出“g property not found!”;
2、__set( $property , $value ) 給未定義的屬性調用時賦值
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 ) );
這裡我們可以很清楚的看到 , 當給未定義的‘name’賦值時 , 會調用“__set()”
其他的還有 __call(), __isset() , __unset();
這裡最有用和最常用的的是__call() , 當調用一個為存在的方法時被調用; __isset()是在對一個為定義的屬性使用isset()函數時被調用, __unset是在對未定義的數以使用unset時被調用
轉自:http://blog.csdn.net/shuiping567541/article/details/7061258