Discuz教程网

[二次开发] 模仿DISCUZ的单列模式链接mysql

[复制链接]
authicon dly 发表于 2012-11-8 19:44:37 | 显示全部楼层 |阅读模式
单列模式:顾名思义就是只new一次 ,其实每次new一个类的时候 内存都会新建一个空地存放那些属性和方法,所以单列模式就很好的防止类重复的被实例化,浪费内存资源。
我这里看着discuzX2的class_core.php文件 慢慢琢磨的。请高手指教。


  1. /* mysql
  2. * class_db.php  @author odaboy
  3. *  
  4. *  */
  5. class odaboy_core{
  6.         var $config;
  7.         var $db;
  8.         function &instance() {//单列模式
  9.                 static $object;
  10.                 if(empty($object)) {
  11.                         $object = new odaboy_core();
  12.                 }
  13.                 return $object;
  14.         }
  15.         
  16.         function init(){
  17.                 global $_M;
  18.                 $this->init_db();
  19. //                init_post();
  20.         }
  21.         function init_db(){
  22.                 require_once 'config/config_global.php';
  23.                 $this->config =$config;
  24.                 $this->db = DB::instance();
  25.                 $this->curlink = DB::connect($this->config);
  26.                 DB::$link = $this->curlink['dbconnect'];
  27.                 DB::$config = $this->config;
  28.         }

  29. }
复制代码




  1. class DB {
  2.         static $link ;
  3.         static $config ;
  4.         function DB(){
  5.                         return true;
  6.         }
  7.         function &instance() {//单列模式
  8.                 static $object;
  9.                 if(empty($object)) {
  10.                         $object = new DB();
  11.                 }
  12.                 return $object;
  13.         }
  14.         function connect($config){
  15.                 $config['dbconnect'] = mysql_connect($config['db']['server'],$config['db']['user'],$config['db']['passwd']) or die("mysql link error");
  16.                 mysql_selectdb($config['db']['table']);
  17.                 mysql_set_charset($config['db']['charset']);
  18.                 return          $config;
  19.         }
  20.         
  21.         function query($sql){
  22.                 $query = mysql_query($sql);
  23.                 return $query;        
  24.         }

  25.         function insert_id(){
  26.                 return mysql_insert_id();
  27.         }
  28.         function insert($arr,$table=''){
  29.                 $arr = is_array($arr) ? $arr : array($arr);
  30.                 if($table='') return false;
  31.                 $sql = "INSERT INTO {$table} VALUES(";
  32.                
  33.                 foreach($arr as $key=>$value){
  34.                         $sql .= " $value ,";
  35.                 }
  36.                 return $this->query(substr($sql, 0,-1).')') or die('mysql_insert_error');
  37.         }
  38.         function update($arr,$table,$where=''){
  39.                 $arr = is_array($arr) ? $arr : array($arr);
  40.                 if($table='') return false;
  41.                 $sql = "UPDATE  {$table} SET";
  42.                 foreach($arr as $key=>$value){
  43.                         $sql .= "{$key} = {$value} ,";
  44.                 }
  45.                 if($where!=''){
  46.                         $where = " WHERE {$where}";
  47.                         $sql = substr($sql, 0,-1).')' . $where;
  48.                 }else{
  49.                         $sql =substr($sql, 0,-1).')';
  50.                 }
  51.                 return $this->query($sql) or die('mysql_insert_error');
  52.         }
  53.         function delete($table,$where=''){
  54.                 if($where!=''){
  55.                         $where = " WHERE {$where}";
  56.                 }
  57.                 $sql = "DELETE FROM `$table` $where";
  58.                 return $this->query($sql) or die('mysql_insert_error');
  59.         }
  60.         function table($str){
  61.                 return $str = DB::$config['db']['pre'] . $str;
  62.         }
  63. }
复制代码



上一篇:转载:世界公园瑞士旅游行程安排
下一篇:Discuz X2利用http提交post,让论坛支持删除指定用户在uc其他论坛应用的信息
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 01:33

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表