单列模式:顾名思义就是只new一次 ,其实每次new一个类的时候 内存都会新建一个空地存放那些属性和方法,所以单列模式就很好的防止类重复的被实例化,浪费内存资源。  
我这里看着discuzX2的class_core.php文件 慢慢琢磨的。请高手指教。  
 
 
- /* mysql
 
 - * class_db.php  @author odaboy
 
 - *  
 
 - *  */
 
 - class odaboy_core{
 
 -         var $config;
 
 -         var $db;
 
 -         function &instance() {//单列模式
 
 -                 static $object;
 
 -                 if(empty($object)) {
 
 -                         $object = new odaboy_core();
 
 -                 }
 
 -                 return $object;
 
 -         }
 
 -         
 
 -         function init(){
 
 -                 global $_M;
 
 -                 $this->init_db();
 
 - //                init_post();
 
 -         }
 
 -         function init_db(){
 
 -                 require_once 'config/config_global.php';
 
 -                 $this->config =$config;
 
 -                 $this->db = DB::instance();
 
 -                 $this->curlink = DB::connect($this->config);
 
 -                 DB::$link = $this->curlink['dbconnect'];
 
 -                 DB::$config = $this->config;
 
 -         }
 
  
- }
 
  复制代码 
 
 
 
- class DB {
 
 -         static $link ;
 
 -         static $config ;
 
 -         function DB(){
 
 -                         return true;
 
 -         }
 
 -         function &instance() {//单列模式
 
 -                 static $object;
 
 -                 if(empty($object)) {
 
 -                         $object = new DB();
 
 -                 }
 
 -                 return $object;
 
 -         }
 
 -         function connect($config){
 
 -                 $config['dbconnect'] = mysql_connect($config['db']['server'],$config['db']['user'],$config['db']['passwd']) or die("mysql link error");
 
 -                 mysql_selectdb($config['db']['table']);
 
 -                 mysql_set_charset($config['db']['charset']);
 
 -                 return          $config;
 
 -         }
 
 -         
 
 -         function query($sql){
 
 -                 $query = mysql_query($sql);
 
 -                 return $query;        
 
 -         }
 
  
-         function insert_id(){
 
 -                 return mysql_insert_id();
 
 -         }
 
 -         function insert($arr,$table=''){
 
 -                 $arr = is_array($arr) ? $arr : array($arr);
 
 -                 if($table='') return false;
 
 -                 $sql = "INSERT INTO {$table} VALUES(";
 
 -                 
 
 -                 foreach($arr as $key=>$value){
 
 -                         $sql .= " $value ,";
 
 -                 }
 
 -                 return $this->query(substr($sql, 0,-1).')') or die('mysql_insert_error');
 
 -         }
 
 -         function update($arr,$table,$where=''){
 
 -                 $arr = is_array($arr) ? $arr : array($arr);
 
 -                 if($table='') return false;
 
 -                 $sql = "UPDATE  {$table} SET";
 
 -                 foreach($arr as $key=>$value){
 
 -                         $sql .= "{$key} = {$value} ,";
 
 -                 }
 
 -                 if($where!=''){
 
 -                         $where = " WHERE {$where}";
 
 -                         $sql = substr($sql, 0,-1).')' . $where;
 
 -                 }else{
 
 -                         $sql =substr($sql, 0,-1).')';
 
 -                 }
 
 -                 return $this->query($sql) or die('mysql_insert_error');
 
 -         }
 
 -         function delete($table,$where=''){
 
 -                 if($where!=''){
 
 -                         $where = " WHERE {$where}";
 
 -                 }
 
 -                 $sql = "DELETE FROM `$table` $where"; 
 
 -                 return $this->query($sql) or die('mysql_insert_error');
 
 -         }
 
 -         function table($str){
 
 -                 return $str = DB::$config['db']['pre'] . $str;
 
 -         }
 
 - }
 
  复制代码 |   
 
上一篇: 转载:世界公园瑞士旅游行程安排下一篇: Discuz X2利用http提交post,让论坛支持删除指定用户在uc其他论坛应用的信息 
 
 |