Using the singleton method to create a global object

meershorn

New Member
I am trying to use the singleton method to access a global object (in this example its "username"). My question is how can I modify this so that in the \[code\]DB->connect()\[/code\] function I could do \[code\]echo $this->username;\[/code\] without declaring $username or changing the last 2 lines?\[code\]class CI_Base { private static $instance; public function CI_Base() { self::$instance =& $this; } public static function &get_instance() { return self::$instance; }}function &get_instance() { return CI_Base::get_instance();}class Foo { function run() { $CI = & get_instance(); $CI->username = "test"; $db = new DB; $db->connect(); }}class DB extends Foo { function connect() { $CI = & get_instance(); echo $CI->username; }}$foo = new Foo;$foo->run();\[/code\]
 
Back
Top