PHP and singletons again
ConfigReader class {private static $ instance = NULL; protected $ configData = array ();
public function getInstance()
{
if( self::$instance == NULL )
{
self::$instance == new ConfigReader();
}
return self::$instance;
}
public function getConfigValue( $getName )
{
echo 'In function';
}
private function __construct()
{
$this->configData = %READ_FROM_DATABASE%;
}
private function __clone() {}
}
And for:
var_dump( ConfigReader::getInstance() )
I got: NULL
I broke my brain ... Help me, please.
+2
Ockonal
source
to share
3 answers
In the getInstance method, you should only use one '=': you want to make assignment, not compatibility:
self::$instance = new ConfigReader();
Instead
self::$instance == new ConfigReader();
And this method must be declared as static
, since you are using it as a static method:
public static function getInstance()
{
if( self::$instance == NULL )
{
self::$instance = new ConfigReader();
}
return self::$instance;
}
You have these two modifications, it should work; -)
+5
Pascal MARTIN
source
to share
Just a typo: self::$instance == new ConfigReader()
contains == instead of =
+6
Xeor
source
to share
The getInstance () method must also be static.
+5
rogeriopvl
source
to share