View Single Post
Old 07-19-2009, 10:18 PM   #2 (permalink)
Enfernikus
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Nay, you're looking now into design patterns, for this sort of occasion perhaps a Registry pattern would suffice


Note: Technically speaking you COULD but it's just bad practice.

php Code:
<?php

final class Registry
{
    private static $lib;
   
        public static function fetchLibrary($lib)
        {
            if( !array_key_exists($lib, self::$lib) )
            {
                self::$lib[$lib] = new $lib;
            }
           
            return self::$lib[$lib];
        }
}

class MySQL
{
    //Coool stuff here
}

class forumApp
{
    private $db, $session, $user;
   
        public function __construct()
        {
            $this->db      = Registry::fetchLibrary('MySQL');
            $this->session  = Registry::fetchLibrary('session');
            $this->user     = Registry::fetchLibrary('user');
        }
}

Alternatively you could something like the following

php Code:
<?php

class MySQL
{
    //Coool stuff here
}

class forumApp
{
    private $db, $session, $user;
   
        public function __construct( MySQL $mysql )
        {
            $this->db     = $mysql;
        }
}

$mysql = new MySQL();
$fApp = new forumApp( $mysql )

Can't remember for the life of me what the pattern is called.
__________________
My Blog
Enfernikus is offline  
Reply With Quote
The Following 2 Users Say Thank You to Enfernikus For This Useful Post:
adamdecaf (07-19-2009), hello-world (09-17-2009)