Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Defining a class in a settings file

    Hi all

    Wonder if you can help. I have some classes for a database controller. The class lets you access the database through the same method names. eg

    class mysqldb {
    public function insertData()
    }

    class postgredb {
    public function insertData()
    }

    So in your main scripts you only need to change the call new to use a different database. eg

    $db = new mysqldb();
    or
    $db = new postgredb();

    While this makes writing your code for a different db much easier is there a way to so something like this :

    // In a settings file:
    $dbaseType = mysqldb();

    // In all other files you call the object with:
    $db = new $dbaseType;

    I know the above does not work but I think it highlights what I'm after.
    I only define the class I want to use for a database controller once in a config file and all the calls are updated from this.

    Cheers
    Steve

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by Cacus View Post
    While this makes writing your code for a different db much easier is there a way to so something like this :

    // In a settings file:
    $dbaseType = mysqldb();

    // In all other files you call the object with:
    $db = new $dbaseType;

    I know the above does not work but I think it highlights what I'm after.
    I only define the class I want to use for a database controller once in a config file and all the calls are updated from this.
    maybe you mean something like a Factory class?
    PHP Code:
    class DBFactory
    {
        public static function 
    create($type "mysql")
        {
            
    // do some checks to verify the type
            // e.g. a switch() statement

            // instantiate class
            
    $classname $type "db";
            return new 
    $classname();
        }
    }

    $db DBFactory::create("mysql"); 
    be sure to protect/cover your API by Interfaces.

    other than that you can save your once instantiated DB class in a Registry or Service Provider at initialisation time to get it once you need it (similar to the Factory, only that you save the type anywhere inside)
    PHP Code:
    // not sure if this is 100% correct
    class DBService
    {
        private static 
    $type NULL;

        public function 
    __construct($type "mysql")
        {
            if (
    NULL !== self::$type)
            {
                throw new 
    UnexpectedValueException("DB type is already set to " self::$type);
            }
            
    // do checks …

            
    self::$type $type;
        }

        public static function 
    getDB()
        {
            
    // prevent accessing unset $type …

            
    $classname $self::$type "db";
            return new 
    $classname();
        }

    Last edited by Dormilich; 11-10-2011 at 10:59 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    Cacus (11-10-2011)

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi

    Thanks for the reply the factory class is just the ticket.

    Steve

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I'd suggest that you interface these classes as well. A good part of the power when it comes to using OO is the ability to tightly control the objects in use. Using a common interface will allow PHP to typehint them when you need to pass them around different methods and functions. The interface is quite typically the bases used when creating factory patterns.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •