PHP Single-instance mode

Source: Internet
Author: User
Tags class definition

PHP Single-instance mode

Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system. That is, a class has only one instance of an object.

The main points are three:

1. A class can have only one object;

2. The object of this class must be created on its own;

3, to the whole system to provide this one object;

From the specific implementation point of view, is the following three points:

One is that the Singleton mode class only provides a private constructor,

The second is that the class definition contains a static private object of the class.

The third is that the class provides a static public function to create or get a static private object of its own.

Of course, there must be a private clone method to prevent cloning;

Advantage one, the instance control singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique instances. Ii. flexibility because classes control the instantiation process, classes can flexibly change the instantiation process. Disadvantage one, although the amount of overhead is very small, it will still require some overhead if you want to check if an instance of the class exists every time the object requests a reference. This problem can be resolved by using static initialization. Ii. possible development confusion when using singleton objects, especially those defined in a class library, developers must remember that they cannot instantiate an object using the New keyword. Because library source code may not be accessible, application developers may unexpectedly find themselves unable to instantiate this class directly. Third, the object lifetime cannot resolve the problem of deleting a single object. In a language that provides memory management, such as a. NET framework-based language, only a singleton class can cause an instance to be deallocated because it contains a private reference to the instance. In some languages, such as C + +, other classes can delete object instances, but this results in a floating reference in a singleton class. (Here is not quite understand, solve) PHP single-Sample code demo:
1<?PHP2 //Single Case3 classuni{4         //Create a static private variable to save the class object5     Static Private $instance;6         //Parameters7     Private $config;8         //prevent direct creation of objects9     Private function__construct ($config){Ten         $thisConfig =$config; One                 Echo"I was instantiated."; A     } -         //prevent cloning Objects -     Private function__clone () { the  -     } -     Static  Public functionGetInstance ($config){ -                 //determine if the $instance is a uni object + //No then create -         if(!self::$instanceinstanceof Self) { +Self::$instance=NewSelf$config); A         } at         returnSelf::$instance; -          -     } -      Public functionGetName () { -         Echo $this-config; -     } in } - $db 1= Uni::getinstance (1); to $db 1-getName (); + Echo"<br>"; - $db 2= Uni::getinstance (4); the $db 2-getName (); *?>

Operation Result:

I was instantiated 1.

1

$db 1 After creating the object, the parameter is 1

$db 2 does not create an object, but instead uses the unique object saved directly by $instance, so the output parameter does not change to 4;

PHP Single-instance mode

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.