how to make objects globally accessible?

Posted by fayer on Stack Overflow See other posts from Stack Overflow or by fayer
Published on 2010-05-08T18:09:34Z Indexed on 2010/05/08 18:18 UTC
Read the original article Hit count: 207

Filed under:
|
|
|

i have this code:

class IC_Core {

    /**
     * Database
     * @var IC_Database
     */
    public static $db = NULL;

    /**
     * Core
     * @var IC_Core
     */
    protected static $_instance = NULL;

    private function __construct() {

    }

    public static function getInstance() {
        if ( ! is_object(self::$_instance)) {
            self::$_instance = new self();
            self::initialize(self::$_instance);
        }
        return self::$_instance;
    }

    private static function initialize(IC_Core $IC_Core) {
        self::$db = new IC_Database($IC_Core);
    }

}

but when i wanna access IC_Database with:

$IC = IC_Core::getInstance();
$IC->db->add() // it says that its not an object.

i think the problem lies in self::$db = new IC_Database($IC_Core);

but i dont know how to make it work.

could someone give me a hand=)

thanks!

© Stack Overflow or respective owner

Related posts about oop

Related posts about design-patterns