PHP單例模式使用方法詳解

來源:互聯網
上載者:User
這篇文章主要介紹了PHP單例模式簡單用法,結合資料庫操作類執行個體形式分析了php單例模式的具體實現方法與提示,需要的朋友可以參考下

具體如下:

<?phpclass db {  public $conn;  public static $sql;  public static $instance=null;  private function __construct(){    require_once('db.config.php');    $this->conn = mysql_connect($db['host'],$db['user'],$db['password']);    if(!mysql_select_db($db['database'],$this->conn)){      echo "失敗";    };    mysql_query('set names utf8',$this->conn);  }  public static function getInstance(){  if(is_null(self::$instance)){    self::$instance = new self();  }    return self::$instance;  }  /**  * 查詢資料庫  */  public function select($table,$condition=array(),$field = array()){    $where='';    if(!empty($condition)){      foreach($condition as $k=>$v){        $where.=$k."='".$v."' and ";      }      $where='where '.$where .'1=1';    }    $fieldstr = '';    if(!empty($field)){      foreach($field as $k=>$v){        $fieldstr.= $v.',';      }      $fieldstr = rtrim($fieldstr,',');    } else {      $fieldstr = '*';    }    self::$sql = "select {$fieldstr} from {$table} {$where}";    $result=mysql_query(self::$sql,$this->conn);    $resuleRow = array();    $i = 0;    while($row=mysql_fetch_assoc($result)){      foreach($row as $k=>$v){        $resuleRow[$i][$k] = $v;      }    $i++;    }    return $resuleRow;  }  //添加一條記錄  public function insert($table,$data) {    $values = '';    $data = '';    foreach ($data as $k=>$v) {      $values .= $k.',';      $datas .= "'$v'".',';    }    $values = rtrim($values,',');    $datas = rtrim($datas,',');    self::$sql = "INSERT INTO {$table} ({$values}) VALUES ({$datas})";    if(mysql_query(self::$sql)) {      return mysql_insert_id();    } else {      return false;    }  }  //修改一條記錄  public function update($table,$data,$condition=array()){    $where='';    if(!empty($condition)) {    foreach($condition as $k=>$v) {      $where.=$k."='".$v."' and ";    }      $where='where '.$where .'1=1';    }    $updatastr = '';    if(!empty($data)) {    foreach($data as $k=>$v) {      $updatastr.= $k."='".$v."',";    }      $updatastr = 'set '.rtrim($updatastr,',');    }    self::$sql = "update {$table} {$updatastr} {$where}";    return mysql_query(self::$sql);  }  //刪除記錄  public function delete($table,$condition) {    $where='';    if(!empty($condition)) {      foreach($condition as $k=>$v) {        $where.=$k."='".$v."' and ";      }      $where='where '.$where .'1=1';    }    self::$sql = "delete from {$table} {$where}";    return mysql_query(self::$sql);  }  public static function getLastSql() {    echo self::$sql;  }}$db = db::getInstance();//$list = $db->select('demo',array('name'=>'tom','password'=>'ds'),array('name','password'));//echo $db->insert('demo',array('name'=>'最近你啦','password'=>'123'));//echo $db->update('demo',array("name"=>'xxx',"password"=>'123'),array('id'=>1));echo $db->delete('demo',array('id'=>'2'));db::getLastSql();echo "<pre>";?>

相關推薦:

php實現mongoDB單例模式操作類步驟詳解

PHP中單例模式與原廠模式詳解及執行個體

PHP單例模式使用案例詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.