drs7798 2014-12-15 00:23
浏览 27
已采纳

数据库连接 - 单例。 如何在其中传递用户数据?

Trying to use database connection with singleton. __constructor has been private and returning instance from static function.

class MyDbConn 
{
    protected $_conn;
    protected $database;
    protected $user;
    protected $password;
    protected $host = "localhost";
         private static $instance;

         private function __construct()
         {
              $this->db_conn = new PDO("mysql:host=$this->host;dbname=$this->database",$this->user,$this->password);
              $this->db_conn->exec("set names utf8");
              $this->_conn = $this->db_conn;
         }

         public static function returnInstance() {
              if(self::$instance == null)
              {
                  print "Object  created!";
                  self::$instance = new self;

              }

               return self::$instance;

          }

           public function returnconnection(){
            return $this->_connection;
          }
     } 

Now I can access the connection instance from MyDbConn::returnInstance()->returnconnection();

But, How do I pass my users detail and database name to this constructor ?? I cannot do ::returnInstance($username,$password) or returnconnection($username,$password).

Any ideas please.

  • 写回答

2条回答 默认 最新

  • douyouchou1085 2014-12-15 01:39
    关注

    You can do it using global or with a normal function call

    <?php
    class MyDbConn 
    {
        protected $_conn;
        protected $database;
        protected $user;
        protected $password;
        protected $host = "localhost";
        private static $instance;
    
        private function __construct()
        {
    
        }
    
        public static function returnInstance() {
             if(self::$instance == null)
             {
                 print "Object  created!";
                 self::$instance = new self;
             }
              return self::$instance;
         }
    
         private static function initDatabase(){
             $database = self::returnInstance();
             global $config;
             $database->_conn = new PDO("mysql:host={$config['host']};dbname={$config['database']}",$config['user'],$config['password']);
             $database->_conn->exec("set names utf8");
             return $database;
         }
    
          public static function returnconnection(){
           try {
               $db = self::initDatabase();
               return $db->_conn;
            } catch (Exception $ex) {
               echo "Error Establishing Database Connection " . $ex->getMessage();
               return null;
            }
          }
    }
    

    and pass the data like this

    // will be called from initDatabase using global keyword
    $config = array('host' => 'localhost', 'database' => 'db name','user' => 'user here','password' => '');
    
    $db = MyDbConn::returnconnection();
    

    I don't recommend Singleton, and i suggest using Dependency Injection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow