单个初始化类

时间:2012-06-20 08:09:06

标签: php oop class initialization

我已经将类的实例存储在类中的一个属性中进行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 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

答案 2 :(得分:0)

您在寻找Singleton pattern吗?


修改

如果要为dbClass实现Singleton,不要调用new dbClass()。而是将dbClass构造函数设置为private或protected,并使用静态方法dbClass::get()(或类似)来检查您是否已有实例。

答案 3 :(得分:0)

普遍的共识是,当你环顾四周时,不能使用单身人士模式而且它是反模式的 stackoverflow有很多相关的问题,最高投票的答案涉及单一设计模式。

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

当页面类扩展baseClass时,不需要调用parent :: __构造,因为解释器向后工作并且将 将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
                )

        )

)*/

?>