Home >Backend Development >PHP Tutorial >PHP implements singleton() singleton mode instance, singleton instance_PHP tutorial
The example in this article describes how to implement singleton() singleton mode in PHP. Share it with everyone for your reference. The specific implementation method is as follows:
common.php file is as follows:
private Singleton s;
private Singleton()
{
}
public static Singleton getSigleton()
{
if(s==null)s=new Singleton();
return s;
}
}
This is a singleton pattern, I think There should be no need for comments. The principle is that the constructor of this class is private and cannot be called from outside, so new Singleton(); cannot be used to get an instance. Then
if you want to get an instance, you have to call its static method getSigleton(); that is Singleton.getSigleton(); will return a Singleton instance. Pay attention to the statements in this method. That is, if you call this method for the first time, it will give you a new instance. All subsequent calls will get this instance. That is to say, there is only one instance of Singleton from beginning to end, which is the singleton mode.
java singleton pattern problem
Local variables in DAO methods are all thread-independent, and there is no problem of concurrency conflicts.