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 DB->connect()
function I could do echo $this->username;
without declaring $username or changing the last 2 lines?
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();