PHP正确的方式与其他类共享类db connect和vars config

时间:2014-07-14 10:11:17

标签: php

我有一些类必须连接到db并获取共享变量,所以我想问你一些有关它的信息。

对于db我尝试创建一个Singletone类。

class Database  
{  
   /**
    * @const Mysqli parameters 
    */
    const HOST = "xxxxx";  
    const USER = "xxxxx";  
    const PASS = "xxxxx";  
    const DB   = "xxxxx";  

   /**
    * @var $instance The class instance
    */
    private static $instance; 

   /**
    * Class constructor
    * block directly instantiating
    */  
    private function __construct() {} 

   /**
    * Class clone
    * block cloning of the object
    */  
    private function __clone() {} 

   /**
    * Initialize Mysqli
    * create the instance if it does not exist
    * @return void 
    */  
    public static function init()  
    {  
         if(!isset(self::$instance))  
         {  
               self::$instance = new Mysqli(self::HOST, self::USER, self::PASS, self::DB);  

              if(self::$instance->connect_error)  
              {  
                     throw new Exception('MySQL connection failed: ' . self::$instance->connect_error);  
              }  
          }  
          return self::$instance;  
    }  
 }

在其他类中以这种方式调用Singletone类

class Users
{
    /**
     * @var $db init database class
     */
     private $db;

    /**
     * Class constructor 
     */
     public function __construct() 
     {
           $this->db = Database::init();
     }

    /**
     * Simple mysql select
     */
     public function simpleSQL() 
     {
         $result = $this->db->query("SELECT * FROM users");
            while($r = $result->fetch_assoc()) 
            {
                  echo $r['email'] . "<br />";
            }
     }
 }
  • 这是正确的方法吗?
  • 我的代码是否正确?

现在关于变量(由db采用)我想创建一个新类

class Config
{
   /**
    * @var $db init database class
    */
    private $db;

   /**
    * @var $public_ip
    * @type string
    */
    public $public_ip;

   /**
    * @var $public_port
    * @type string
    */
    public $public_port;

   /**
    * @var $manage_ip
    * @type string
    */
    public $manage_ip;

   /**
    * @var $manage_port
    * @type string
    */
    public $manage_port;

   /**
    * Class constructor 
    */
    public function __construct() 
    {
        $this->db = Database::init();
        $this->setVars();
    }

   /**
    * Set variables "global"
    * @return @void
    */
    public function setVars() 
    {
        $result = $this->db->query("SELECT * FROM config WHERE id = 1");
         while($r = $result->fetch_assoc()) 
        {
              $this->public_ip  = $r['public_ip'];
              $this->public_port  = $r['public_port'];
              $this->manage_ip  = $r['manage_ip'];
              $this->manage_port    = $r['manage_port'];
        }
   }
}

然后以这种方式在其他类中初始化

class Users
{
private $db;
private $cnf;

/**
 * Class constructor 
 */
 public function __construct() 
 {
     $this->db = Database::init();
     $this->cnf = new Config();
 }

/**
 * Simple mysql select
 */
 public function simpleSQL() 
 {
     $result = $this->db->query("SELECT * FROM users");
         while($r = $result->fetch_assoc()) 
         {
             echo $r['email'] . "<br />";
         }
 }

/**
 * Get vars
 */
 public function getVars() 
 {
     echo $this->cnf->public_ip . "<br />";
     echo $this->cnf->public_port . "<br />";
     echo $this->cnf->manage_ip . "<br />";
     echo $this->cnf->manage_port . "<br />";
 }
}

class foo
{

 private $db;
 private $cnf;

 /**
  * Class constructor 
  */
 public function __construct() 
 {
     $this->db = Database::init();
     $this->cnf = new Config();
 }

/**
 * Get vars
 */
 public function getVars() 
 {
     echo $this->cnf->public_ip . "<br />";
     echo $this->cnf->public_port . "<br />";
     echo $this->cnf->manage_ip . "<br />";
     echo $this->cnf->manage_port . "<br />";
 }
}
  • 这是正确的方法吗?
  • 这样做的正确方法是什么?

由于

1 个答案:

答案 0 :(得分:0)

最佳方法是创建一个执行实际数据库连接并执行查询的类。然后,在具有数据访问权限的所有类之间共享此Connector类。

这个cass不应该是静态或单例类。只需在应用程序启动时将其实例化,并将其提供给需要它的所有类。也许在构造函数中,或者使用专用的setter更好。

$Connector = new Connector("user", "pass", "other stuff from config");

$Users = new Users($Connector);
$Users->doStuff();

class Users
{
    private $connector;

    public function __construct($connector) 
    {
        $this->connector = $connector
    }

    public function doStuff(){
        $result = $this->connector->query("SELECT STUFF");
    }
}

使用此方法,您可以将连接器类更改或替换为其他数据库,而无需在应用中更改任何内容。为奖励点实现接口和一些依赖注入(策略模式?)。