PHP OOP singleton doesn't return object
- by Misiur
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.