Problem
This is the first time I’ve ever seen code like this:
public static function getInstance()
{
if ( ! isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
Is new className() the same as this?
EDIT
Which class does it point to if the class is inheritant?
Asked by user198729
Solution #1
The class in which it is written is indicated by self.
So, if your getInstance method is in a class called MyClass, write this:
self::$_instance = new self();
Will act in the same way as:
self::$_instance = new MyClass();
After the comments, I’ve added a little additional information.
There are two scenarios when you have two classes that extend one other:
The first scenario would look like this (for this example, I’ve removed all non-necessary code; you’ll need to add everything back to obtain the singleton behavior):
class MyParentClass {
}
class MyChildClass extends MyParentClass {
public static function getInstance() {
return new self();
}
}
$a = MyChildClass::getInstance();
var_dump($a);
Here’s what you’ll find:
object(MyChildClass)#1 (0) { }
What does “self” mean? MyChildClass is the name of the class where it is written.
class MyParentClass {
public static function getInstance() {
return new self();
}
}
class MyChildClass extends MyParentClass {
}
$a = MyChildClass::getInstance();
var_dump($a);
And you’d get something like this:
object(MyParentClass)#1 (0) { }
What does “self” mean? MyParentClass — i.e. here too, the class in which it is written.
class MyParentClass {
public static function getInstance() {
return new static();
}
}
class MyChildClass extends MyParentClass {
}
$a = MyChildClass::getInstance();
var_dump($a);
But, with static instead of self, you’ll now get :
object(MyChildClass)#1 (0) { }
That is, static refers to the class that is used (in this case, MyChildClass::getInstance()), not the one in which it is written.
Of course, the behavior of self has not been changed, to not break existing applications — PHP 5.3 just added a new behavior, recycling the static keyword.
Answered by Pascal MARTIN
Solution #2
This appears to be a Singleton pattern implementation. The function is called statically and checks whether the variable $_instance is set in the static class.
If it isn’t, it creates a new self instance (new self()) and puts it in $_instance.
The objective of the singleton pattern is that every time you call className::getInstance(), you’ll get the same class instance.
But I’ve never seen it done like this before, and I honestly didn’t think it was feasible. In the class, what is $_instance specified as?
Answered by Pekka
Solution #3
The double colon (::) operator can access members that are declared static inside the class, so if there are static members, the pseudo variable $this cannot be used, so the code used self instead. Singletons are good programming practices that will only allow one instance of an object, such as database connector handlers. Accessing that instance from client code would be accomplished by creating a single access point, which he named getInstance() in this case. getInstance was the function that created the object by essentially using the new keyword to create an object, implying that the constructor method was also called.
The line if(!isset(self::instance)) checks if an object has already been formed; you may not understand this because the code is only a fragment; there should be static members like presumably someplace near the top.
private static $_instance = NULL;
We would have accessed this member in typical classes by just typing his or her name.
$this->_instance = 'something';
We couldn’t utilize the $this code we use instead because it was marked static.
self::$_instance
The class can decide whether or not to create a single instance by checking if an object is stored on this static class variable. If it is not set,!isset, meaning no object exists on the static member $_instance, then it generates a new object and stores it in the static member $_instance by the command.
self::$_instance = new self();
It was then returned to the client code. The client code can then utilize the object’s public methods to retrieve the one instance, but invoking the single access point, that is, the getInstance() function, is similarly complex; it must be called like this.
$thisObject = className::getInstance();
The reason for this is that the function is marked static.
Answered by flimh
Solution #4
Yes, it’s similar to new className() (which refers to the class that contains that method), which is usually used in a Singleton style with a private constructor.
Answered by Matteo Riva
Solution #5
If the class is inherited, you will not get a child instance if you call getInstance() from child. It will only return a parent instance instance. This is due to the fact that we refer to the new self as such ().
If you want the child class to return an instance of the child class, use new static() in the getInstance() method, and the child class instance will be returned. This is referred to as late binding!!
Answered by kta
Post is based on https://stackoverflow.com/questions/2396415/what-does-new-self-mean-in-php