一、属性重载
 /* 属性重载
		在给不可访问属性赋值时,__set() 会被调用。 
		读取不可访问属性的值时,__get() 会被调用。 
		当对不可访问属性调用 isset()  或 empty()  时,__isset() 会被调用。 
		当对不可访问属性调用 unset()  时,__unset() 会被调用。 
     */ 
    
    //不可访问即是不存在或者是受访问控制修饰符的限制
    //__set中也只给用户设置一些基本连接信息,$name是不可访问属性的名,$value为值
		public function __set($name,$value){
            $allow_set = array(‘host‘,‘port‘,‘user‘,‘pass‘,‘charset‘,‘dbname‘);
            if(in_array($name,$allow_set)){
                 $this->$name = $value;
            }
		}
//__get中也只给用户看一些基本连接信息,$name是不可访问属性的名
        public function __get($name){
        	$allow_get = array(‘host‘,‘port‘,‘charset‘,‘dbname‘);
        	if(in_array($name,$allow_get)){
                 return $this->$name;
        	}else{
        		 return NULL;
        	}
        }
    //__isset只给用户查询一些设置的连接属性	
        public function __isset($name){
        	$allow_isset = array(‘host‘,‘port‘,‘user‘,‘pass‘,‘charset‘,‘dbname‘);
        	if(in_array($name,$allow_isset)){
        		return true;
        	}else{
        		return false;
        	}
        }
      //__unset什么都不做 代表不能删除任何属性
        public function __unset($name){
}
二、单例模式


测试

结果如下:

单例模式的作用是无论用户NEW多少次,只开辟一个内存空间,节约了系统资源。
原文:http://www.cnblogs.com/zzmgg/p/6172122.html