View Single Post
Old 08-23-2009, 09:48 PM   #4 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

As you've already identified a solution, we might as well stick to it. That being the singleton pattern. This would work like so. With the following database class:

php Code:
class Database
{
    private static $m_pInstance;
   
    /**
     * @return Database
     */

    public static function getInstance()
    {
        if (!isset(self::$m_pInstance))
        {
            self::$m_pInstance = new self();
        }
       
        return self::$m_pInstance;
    }
   
    public function query($szSQL)
    {
        printf("Executing query: %s<br />", $szSQL);
    }
}

And then we can use it like so from any other class we wish:

php Code:
class MyClass
{
    public function __construct()
    {
        Database::getInstance()->query('SELECT * FROM myTable');
    }
}

new MyClass();
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote