首页 > 后端开发 > php教程 > PHP单例模式的简单用法分享

PHP单例模式的简单用法分享

小云云
发布: 2023-03-20 09:26:01
原创
1348 人浏览过

本文主要和大家介绍了PHP单例模式简单用法,结合数据库操作类实例形式分析了php单例模式的具体实现方法与使用技巧,需要的朋友可以参考下,希望能帮助到大家。


<?php
class db {
  public $conn;
  public static $sql;
  public static $instance=null;
  private function __construct(){
    require_once(&#39;db.config.php&#39;);
    $this->conn = mysql_connect($db[&#39;host&#39;],$db[&#39;user&#39;],$db[&#39;password&#39;]);
    if(!mysql_select_db($db[&#39;database&#39;],$this->conn)){
      echo "失败";
    };
    mysql_query(&#39;set names utf8&#39;,$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=&#39;&#39;;
    if(!empty($condition)){
      foreach($condition as $k=>$v){
        $where.=$k."=&#39;".$v."&#39; and ";
      }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    $fieldstr = &#39;&#39;;
    if(!empty($field)){
      foreach($field as $k=>$v){
        $fieldstr.= $v.&#39;,&#39;;
      }
      $fieldstr = rtrim($fieldstr,&#39;,&#39;);
    } else {
      $fieldstr = &#39;*&#39;;
    }
    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 = &#39;&#39;;
    $data = &#39;&#39;;
    foreach ($data as $k=>$v) {
      $values .= $k.&#39;,&#39;;
      $datas .= "&#39;$v&#39;".&#39;,&#39;;
    }
    $values = rtrim($values,&#39;,&#39;);
    $datas = rtrim($datas,&#39;,&#39;);
    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=&#39;&#39;;
    if(!empty($condition)) {
    foreach($condition as $k=>$v) {
      $where.=$k."=&#39;".$v."&#39; and ";
    }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    $updatastr = &#39;&#39;;
    if(!empty($data)) {
    foreach($data as $k=>$v) {
      $updatastr.= $k."=&#39;".$v."&#39;,";
    }
      $updatastr = &#39;set &#39;.rtrim($updatastr,&#39;,&#39;);
    }
    self::$sql = "update {$table} {$updatastr} {$where}";
    return mysql_query(self::$sql);
  }
  //删除记录
  public function delete($table,$condition) {
    $where=&#39;&#39;;
    if(!empty($condition)) {
      foreach($condition as $k=>$v) {
        $where.=$k."=&#39;".$v."&#39; and ";
      }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    self::$sql = "delete from {$table} {$where}";
    return mysql_query(self::$sql);
  }
  public static function getLastSql() {
    echo self::$sql;
  }
}
$db = db::getInstance();
//$list = $db->select(&#39;demo&#39;,array(&#39;name&#39;=>&#39;tom&#39;,&#39;password&#39;=>&#39;ds&#39;),array(&#39;name&#39;,&#39;password&#39;));
//echo $db->insert(&#39;demo&#39;,array(&#39;name&#39;=>&#39;最近你啦&#39;,&#39;password&#39;=>&#39;123&#39;));
//echo $db->update(&#39;demo&#39;,array("name"=>&#39;xxx&#39;,"password"=>&#39;123&#39;),array(&#39;id&#39;=>1));
echo $db->delete(&#39;demo&#39;,array(&#39;id&#39;=>&#39;2&#39;));
db::getLastSql();
echo "<pre class="brush:php;toolbar:false">";
?>
登录后复制

相关推荐:

php实现的mongoDB单例模式实例操作分享

php实现单例模式的方法

php设计模式中工厂模式和单例模式的区别

以上是PHP单例模式的简单用法分享的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板