【问题标题】:Trying to create a database connection with PDO and singleton pattern尝试使用 PDO 和单例模式创建数据库连接
【发布时间】:2014-01-28 21:06:04
【问题描述】:

我一直在尝试使用 PDO 和单例创建数据库连接。我想我已经完成了,但我不确定我是否使用了正确的单例模式。

我也不确定我是否正确使用了__clone()__wakeup() 方法。而且我没有测试它们的知识。

谁能告诉我我的方法是否正确以及我是否正确使用了单例模式?我对设计模式很陌生。

这是我的代码:

<?php

require_once 'config.php';

class dbConn{

// Variable to store connection object.
protected static $db;

// Assign variables from config.php.
private $host   = DB_HOST;
private $dbuser = DB_USER;
private $dbpass = DB_PASS;
private $dbname = DB_NAME;

// Private construct - class cannot be instatiated externally.
private function __construct() {

    try {
        // Try to create PDO object to the $db variable.
        $pre = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        self::$db = new PDO($pre, $this->dbuser, $this->dbpass);
        self::$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    }
    // If not able to connect to database.
    catch (PDOException $e) {
        echo "Could not connect: " . $e->getMessage();
    } 
}

// Get connection function.
public static function getConnection() {

// Only if no connection object exists it creates one, because we only want one instance.
    if (!self::$db) {
// New connection object.
        new dbConn();
}
    return self::$db;
}

public function __clone() {
        return false;
    }

public function __wakeup(){
        return false;
    }
}

$db = dbConn::getConnection()

?>

【问题讨论】:

    标签: php design-patterns pdo singleton


    【解决方案1】:

    仅供您了解:

    依赖关系是这样的模式:

    class Class {
       private $dependency;
       public function __construct($dep) {
          $this->$dependency = $dep;
       }
    
       function use_dependency( ... ) {
          $dep = $this->$dependency;
          ...
          $dep->action();
          ...
        }
    }
    
    $dep = new Dependency();
    $c = new Class( $dep );
    

    单例模式是这样的:

    class Singleton
    {
        protected static $instance = null;
        protected function __construct()
        {
            //Thou shalt not construct that which is unconstructable!
        }
        protected function __clone()
        {
            //Me not like clones! Me smash clones!
        }
    
        public static function getInstance()
        {
            if (!isset(static::$instance)) {
                static::$instance = new static;
            }
            return static::$instance;
        }
    }
    

    但我再说一遍,这是不好的做法。

    还要注意 __wakeup 在反序列化之后调用(请参阅http://www.php.net/manual/en/language.oop5.magic.php

    【讨论】:

      【解决方案2】:

      这不是单例模式:php 中的单例是一个将自身的唯一实例存储在静态变量中的类。

      恕我直言,最好不要使用单例类,静态类可以做同样的事情。

      对于您想要获得的内容,您可以查看依赖项或

      创建这样的东西:

      class Database extends PDO {
         private $engine = 'mysql';
         private $host = 'localhost';
         private $database = 'Crumbs';
         private $user = 'root';
         private $pass = 'adminuser';
      
         public function __construct()
         {
            $dns = $this->engine.':dbname='.$this->database.";host=".$this->host;
            parent::__construct( $dns, $this->user, $this->pass );
         } 
      }
      

      然后使用:

      $db = new Database();
      

      【讨论】:

        猜你喜欢
        • 2015-01-07
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-02
        • 1970-01-01
        • 2018-12-16
        相关资源
        最近更新 更多