2

我已经将一个类的实例存储在一个类内的一个属性中以进行 void 多重初始化,(希望这是有道理的)。

看起来像这样:

class baseClass
{
    public $db;

    public function __construct()
    {
         $this->db =  new dbClass(); // this class is just a query class
    }   

}


class page  extends baseClass {

   public function __construct()
   {
           parent::__construct();
       }

       public function index()
       {
         // this works very fine and return result what i want //
         $user = $this->db->query('blah blah');


         // my problem is when i use the same property ($this->db) which 
         //contains the dbClass()

         $mesg = $this->db->query('blah blah');

         // the result of both query merge together
         // i think i figured out whats the problem here but i dont know what to do
         // i think i need to reset or the destroy the instance of $this->db 
         // and back it to null values


         // or when i put print_r($this->db) after my first query (the  $users )
         // it display the previous property i set (ofcourse)
         // what i want is.. every time i use the $this->db, i want 
         // it like the first time i use it, or fresh.
         // is there any way ? 
         // thanks for the help experts ;) 


       }
}
4

4 回答 4

2

问题在于您的dbClass实施。似乎它不能使这两个查询彼此分开。

您应该阅读代码dbClass并了解如何分别运行两个查询;如果这是不可能的,您应该至少能够在开始另一个查询结果之前完成一个查询结果。

于 2012-06-20T08:23:36.813 回答
1

这是单例(反)设计模式的经典示例。

http://en.wikipedia.org/wiki/Singleton_pattern

http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html

干杯

于 2012-06-20T08:13:26.670 回答
0

普遍的共识是不使用单例模式,并且它是一种反模式,但是当您查看 stackoverflow 时,有许多相关问题,最高投票的答案涉及单例设计模式。

这就是我更喜欢使用依赖注入的方式,如果一个类需要数据库连接,则通过构造函数将连接传递给该类。

由于页面类扩展了 baseClass,因此无需调用 parent::__construct,因为解释器向后工作并将 baseClass 的构造包含到页面类中。

所以:

<?php 
//Your abstract baseClass this class will be invoked by any clas that 
//extends it
Abstract Class baseClass {
    protected $db;

    function __construct(PDO $db) {
        $this->db = $db;
        //do your inital setup
    }

    //all controllers must contain an index method
    abstract function index();
}

//Your page controller class that is invoked by your router
class page extends baseClass {

    //The parents class's __construct is inherited

    public function index(){
        $this->user = $this->db->query('SELECT username FROM testing')->fetchAll(PDO::FETCH_ASSOC);
        $this->other = $this->db->query('SELECT somthing FROM testing')->fetchAll(PDO::FETCH_ASSOC);
    }
}



try{
    $pdo = new PDO("mysql:host=localhost;dbname=test_db", 'root', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (Exception $e){
    die('Cannot connect to mySQL server.');
}

//This below will be controlled by your router class

//Pass the PDO object to the constructor of baseClass
$page = new page($pdo);
$page->index();

print_r($page);
/*page Object
(
    [db:protected] => PDO Object
        (
        )

    [user] => Array
        (
            [0] => Array
                (
                    [username] => test
                )

            [1] => Array
                (
                    [username] => dasdadsa
                )

        )

    [other] => Array
        (
            [0] => Array
                (
                    [somthing] => test
                )

            [1] => Array
                (
                    [somthing] => eqweqwe
                )

        )

)*/

?>
于 2012-06-20T09:12:19.540 回答
0

您在寻找单例模式吗?


编辑

如果要为 dbClass 实现 Singleton,请不要调用new dbClass(). 而是将 dbClass 构造函数设为私有或受保护,并使用静态方法dbClass::get()(或类似方法)检查​​您是否已经拥有实例。

于 2012-06-20T08:14:59.260 回答