I was looking for info about singleton pattern, I found: http://www.php.net/manual/en/language.oop5.patterns.php#95196
I don’t understand:
final static public function getInstance()
{
static $instance = null;
return $instance ?: $instance = new static;
}
If it set $instance to null, why this kind of return? Why do not create $instance in the global “space” of the class without set it to null in getInstance?
You cannot initiate class variables with non-static values, so
is not permitted.
The code you’ve posted is one way of going about making sure that only ONE instance of that class is defined.
will create the variable and set it to
nullthe first time that method is called. After that, sicne it’s been declaredstatic, PHP will ignore that line.Then the other code can be seen as the following: