Home  >  Article  >  Backend Development  >  PHP implements singleton() singleton mode instance, singleton instance_PHP tutorial

PHP implements singleton() singleton mode instance, singleton instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:541016browse

php implements singleton() singleton mode instance, singleton instance

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:

Copy code The code is as follows:
class CC
{
private static $ins;
public static function singleton()
{
If (!isset(self::$ins)){
               $c = __CLASS__;
                                                                     self::$ins = new $c;                                                                                                                 return self::$ins; 
}  
public function EventResult($Id)
{
return $Id;
}
}
?>

The index.php file is as follows:


Copy code The code is as follows:
                                                                                                               


require 'common.php';
$objCC=CC::singleton();
$r=$objCC->EventResult(7);
print_r($objCC);
echo $r."
";
?>



I hope this article will be helpful to everyone’s PHP programming design.

For singleton mode in JAVA

package test;
public class Singleton {

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

In the code you gave, the singleton is not synchronized
Dao generally uses the singleton mode
In the DAO class, the member variables of the class will be concurrency safe as you said, but in general DAO Class variables are all related to the data source connection and are generally the same, so even concurrent calls will not affect it.

Local variables in DAO methods are all thread-independent, and there is no problem of concurrency conflicts.



http://www.bkjia.com/PHPjc/907283.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907283.htmlTechArticlephp implements singleton() singleton mode example, singleton example This article tells the example of php implementing singleton() singleton mode method. Share it with everyone for your reference. The specific implementation method is as follows:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn