PHP OOP singleton doesn't return object

Posted by Misiur on Stack Overflow See other posts from Stack Overflow or by Misiur
Published on 2010-11-27T23:49:07Z Indexed on 2010/12/25 6:54 UTC
Read the original article Hit count: 229

Filed under:
|
|

Weird trouble. I've used singleton multiple times but this particular case just doesn't want to work. Dump says that instance is null.

define('ROOT', "/");
define('INC', 'includes/');
define('CLS', 'classes/');

require_once(CLS.'Core/Core.class.php');

$core = Core::getInstance();

var_dump($core->instance);

$core->settings(INC.'config.php');
$core->go();

Core class

class Core
{
    static $instance;

    public $db;
    public $created = false;

    private function __construct()
    {
        $this->created = true;
    }   

    static function getInstance()
    {       
        if(!self::$instance) {
            self::$instance = new Core();
        } else {
            return self::$instance;
        }
    }

    public function settings($path = null)
    {
        ...
    }
    public function go()
    {
        ...
    }

}

Error code

Fatal error: Call to a member function settings() on a non-object in path

It's possibly some stupid typo, but I don't have any errors in my editor. Thanks for the fast responses as always.

© Stack Overflow or respective owner

Related posts about php

Related posts about oop