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