2

I have store a instance of a class in a property inside a class to a void multiple initialization, (hope that make sense).

look like this:

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 ;) 


       }
}
dan-lee
  • 13,814
  • 5
  • 48
  • 72
avien
  • 167
  • 5
  • 14
  • further reading: http://martinfowler.com/articles/injection.html or *dependency injection* in general. – Yoshi Jun 20 '12 at 08:13

4 Answers4

2

The problem is with your dbClass implementation. It seems that it cannot keep the two queries separate from each other.

You should read the code of dbClass and find out how can you run two queries separately; if this is not possible you should be able to at least finish off one query result before you start another.

Ja͢ck
  • 161,074
  • 33
  • 239
  • 294
1

This is a classical example of the Singleton (anti-)design pattern.

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

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

Cheers

smassey
  • 5,479
  • 20
  • 34
0

Are you looking for the Singleton pattern?


EDIT

If you want to implement Singleton for dbClass, don't call new dbClass(). Instead make the dbClass constructor private or protected, and have a static method dbClass::get() (or similar) that checks to see if you have an instance already.

  • yes i think, is there any way to solve this problem? without revising all my codes? – avien Jun 20 '12 at 08:17
  • 2
    @avien before you jump on the singleton-wagon, please read up on the many problems with this approach. for example: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Yoshi Jun 20 '12 at 08:18
  • @Yoshi is right to point out the downsides of Singleton. If you decide you need it, I've updated my answer to point out how you would go about it. – michaelhagedon Jun 20 '12 at 08:30
0

The general consensus is that singleton patterns are not tobe used and its an anti-patern, tho when you look around stackoverflow there are many related questions that the top voted answers involve a singleton design pattern.

This is how I would prefer todo it, with dependency injection, if a class requires a database connection then pass the connection to the the class through the constructor.

As page class extends baseClass there is no need to invoke the parent::__construct as the interpreter works backwards and will include baseClass's construct into page class.

So:

<?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
                )

        )

)*/

?>
Lawrence Cherone
  • 41,907
  • 7
  • 51
  • 92