07-19-2009, 10:18 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
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:
<?phpfinal 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:
<?phpclass 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.
|
|
|
|