Re: PHP5 objects access/instantiation model (correction)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2007-07-11 at 16:46 +0100, Steve Perkins wrote:
>
> I guess there are lots of "workaround" ways. I could write lots of
> "middleman" code in generic and use things like __set and __get etc but its
> lots of extra overhead. I also know I could use extends but that makes the
> code the wrong way around (MySQL_Driver would have to extend generic), hence
> the top-level application would have to include code to determine which
> driver to create rather than the db object determining its own connection
> driver to use. This is true also for just lumping everything in a single
> class per db type.

You want a factory class that creates an instance of the appropriate
driver object. The driver object SHOULD extend the generic class.
<?php

class DbFactory
{
    function __construct()
    {
        // :)
    }

    function getConnection( $type, $params )
    {
        if( $type == 'MySQL' )
        {
            return new DB_Driver_MySQL;
        }
        else
        if( $type == 'ODBC' )
        {
            return new DB_Driver_ODBC;
        }

        return false;
    }
}

class DB_Driver_Generic
{
    public $var1;
    public $var2;

    function __construct()
    {
    }

    function runQuery( $query )
    {
    }
}

class DB_Driver_MySQL extends DB_Driver_Generic
{
    function __construct()
    {
        parent::__construct();
    }
}

class DB_Driver_ODBC extends DB_Driver_Generic
{
    function __construct()
    {
        parent::__construct();
    }
}

?>

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux