Singleton () mode instance in php, singleton instance _ PHP Tutorial

Source: Internet
Author: User
Php implements singleton () singleton mode instance and singleton instance. Php implements singleton () singleton mode instance. singleton instance this article describes how to implement singleton () singleton mode in php. Share it with you for your reference. The specific implementation method is as follows: php implements singleton () singleton mode instance and singleton instance

This example describes how to implement singleton () singleton mode in php. Share it with you for your reference. The specific implementation method is as follows:

The common. php file is as follows:

The code is as follows:

<? Php
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:

The code is as follows:



Test



<? Php
Require 'common. php ';
$ ObjCC = CC: singleton ();
$ R = $ objCC-> EventResult (7 );
Print_r ($ objCC );
Echo $ r ."
";
?>

I hope this article will help you with PHP programming.


For the 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 mode. I don't think I need to comment it out. The principle is that the constructor method of this class is private. if it is not called outside, it cannot be new Singleton, so
If you want to get an instance, you have to call its static method getSigleton (); that is, Singleton. getSigleton (); returns 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, this instance will be called later, that is, there will be only one Singleton instance from the consumer, which is the Singleton mode.

Java singleton

In the code you provided, the singleton is not synchronous.
Dao is also a singleton mode.
In the DAO class, the member variables of the class will appear as you mentioned in concurrency security, but generally the DAO class variables are related to the data source connection, which is usually the same, therefore, even concurrent calls are not affected.
Local variables in the DAO method are independent of threads, and there is no concurrency conflict.

Singleton is a singleton. Share it with you for your reference. The specific implementation method is as follows :...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.