Singleton_单例模式_PHP语言描述

使用单例模式来封装数据库操作,主要是为了解决在项目应用中,过多的使用NEW来初始化数据库对象的问题。有效的使用服务器的内存资源。更重要的是体会单例模式的特点及用法。

<?php
 class sqlObj{
  //单例模式用的私有静态对象
  private static $instance;
  //用于记录当前的数据库信息
  private static $localhost;
  //数据库配置信息
  private static $configArr = array(
   'location'=>array('ip'=>'127.0.0.1:3306','user'=>'root','passwd'=>'11111111','database'=>'phpcms'),
     'bbs'=>array('ip'=>'127.0.0.1:3306','user'=>'root','passwd'=>'11111111','database'=>'newvbb'),
     'bussiness'=>array('ip'=>'127.0.0.1:3306','user'=>'root','passwd'=>'11111111','database'=>'bussiness')
  );
  //一些操作MYSQL的变量
  var $link,$query,$id,$result;
  //私有构造函数
  private function __construct($host="location")   
  {
   $this->link = mysql_connect(self::$configArr[$host]['ip'],self::$configArr[$host]['user'],self::$configArr[$host]['passwd']);
   $this->selectdb(self::$configArr[$host]['database']);
   self::$localhost = $host;
  }
  
  function selectdb($dbname="mambo")
  {
   if(!$this->link)
   {
    trigger_error("LINK DATABASE ERROR!",E_USER_ERROR);
   }
   else 
   {
    $bools = mysql_select_db($dbname,$this->link);
    if(!$bools)
    {
     trigger_error("SELECT DATABASE ERROR!",E_USER_ERROR);
    }
   }
  }
  //用于获取单例对象的静态方法
  public static function getInstance($host='location'){
   if (self::$instance instanceof sqlObj) {
    if(self::$instance->localhost == $host)
    {
     //echo 'same link';
     return self::$instance;
    }
    else 
    {
     //echo 'different link';
     self::$instance->link = mysql_connect(self::$configArr[$host]['ip'],self::$configArr[$host]['user'],self::$configArr[$host]['passwd']);
     self::$instance->selectdb(self::$configArr[$host]['database']);
     return self::$instance;
    }
   }
   else{
    return self::$instance = new sqlObj($host);
   }
  }
  
  //测试数据
  function __toString(){
   return "<br>".$this->query."<br>".$this->result."<br>".$this->link."<br>";
  }
  
  //禁止通过CLONE方法复制新的对象
  function __clone(){
   trigger_error("This Object don't used clone function!",E_USER_ERROR);
  }  
  
  //下面都是一些公用的函数封装
  function query($sqlQuery){
   if(!isset($sqlQuery) || trim($sqlQuery) == '')
   {
    trigger_error("NOT INPUT SQL",E_USER_ERROR);
   }
   else 
   {
    $this->query = $sqlQuery;
    $this->result = mysql_query($sqlQuery);
    return $this->result;
   }
  }
   
  function num_rows($sqlQuery)
  {
   if(!isset($sqlQuery) || trim($sqlQuery) == '')
   {
    trigger_error("NOT INPUT SQL",E_USER_ERROR);
   }
   else 
   {
    $this->result = mysql_num_rows($sqlQuery);
    return $this->result;
   }
  }
   
  function limitsQuery($sqlQuery,$curId,$setp)
  {
   if(!isset($sqlQuery) || trim($sqlQuery) == '')
   {
    trigger_error("NOT INPUT SQL",E_USER_ERROR);
   }
   else 
   {
    $sqlQuery = $sqlQuery . " limit " . $curId . "," . $setp;
    $this->result = mysql_query($sqlQuery);
    return $this->result;
   }
  }
   
   function close_free_result($result)
   {
    return mysql_free_result($this->result);
   }
  
    function get_fetch_array()
   {
    return mysql_fetch_array($this->result);
   }
   
   function get_num_rows()
   {
    return mysql_num_rows($this->result);
   }
   
   function _get_fetch_Array($type)
   {
    return mysql_fetch_array($this->result,$type);
   }
   
  function getQuery()
  {
   return $this->query;
  }
   
  function get_insert_id()
  {
   return mysql_insert_id();
  }
   
  function close()
  {
   mysql_close($this->link);
  }
  
 }
 
 $sqlObj = sqlObj::getInstance("bussiness");
 $sqlObjs = sqlObj::getInstance();
?>


你可能感兴趣的:(mysql,Singleton,PHP,单例模式)