Benefit analysis of CI framework using single-case pattern design

Source: Internet
Author: User
In the process of using the CI framework, you will find that the $CI super variable is passed by reference to the value of the way you get through get_instance () is always the same CI object,

Here are some of the benefits of doing this:

The singleton mode is used as a duty mode, which he uses to create a single functional access point in the application.

It delegates control over the creation of objects to a single access point. At any time, only one instance of this class exists in the application.
This can prevent us from opening multiple connections to the database or unnecessarily using redundant system resources.
In more complex systems, using Singleton mode is especially useful for maintaining synchronization of application state.

All singleton classes have at least the following three common elements: they must have a constructor and must be marked private. They have a static member variable that holds an instance of the class.

They have a common static method that accesses this instance and the common class differs from the following:


Singleton classes cannot be instantiated directly in other classes. A singleton class can only be instantiated by itself.
To obtain such a result, the __construct () method must be marked private. If you attempt to construct a class with the private constructor, you will get an accessibility level error.

For a singleton class to work, you must make it available to other classes with an instance that invokes various methods.
The Singleton class does not create a copy of the instance, but instead returns a reference to the instance stored inside the singleton class.
The result is that the Singleton class does not repeatedly consume memory and system resources, allowing other parts of the application to make better use of those resources.
As part of this pattern, you must create an empty private __clone () method to prevent the object from being copied or cloned. This method of returning an instance reference is usually named Gettnstance (). This method must be static, and if it has not yet been instantiated, it must be instantiated.

The getinstance () method can detect whether a class has been instantiated by using the instanceof operator and the self keyword.

This article is provided by topic.alibabacloud.com and explains why you should use singleton mode.

Article Address: http://www.php.cn/php-weizijiaocheng-377485.html

Learn programming to topic.alibabacloud.com www.php.cn

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.