Singleton mode provides access to its own shared instances. Singleton design mode restricts the creation of a specific object only once. Use cases such as database instances generally adopt the Singleton mode. The Singleton mode can reduce the instantiation code of the class: source InitPHP framework, first checks whether the class is not instantiated, the actual... SyntaxHighlighter. all (); Singleton mode
By providing access to its own shared instances, the singleton design mode is used to restrict the creation of a specific object only once.
Use cases
For example, database instances generally adopt the Singleton mode.
The Singleton mode can reduce class instantiation.
Code: source InitPHP framework. First, check whether the class has been instantiated. if it is instantiated, use the object instance that has been stored in the static variable. if it is not instantiated, save the object.
[Php]
/**
* Core Framework loading-all the classes of the framework must pass through this function.
* 1. Singleton mode
* 2. class files in-Controller, Service, View, Dao, Util, and Library can be loaded.
* 3. the framework loads core functions.
* Usage: $ this-> load ($ class_name, $ type)
* @ Param string $ class_name class name
* @ Param string $ type category
*/Www.2cto.com
Public function load ($ class_name, $ type ){
$ Class_path = $ this-> get_class_path ($ class_name, $ type );
$ Class_name = $ this-> get_class_name ($ class_name );
If (! File_exists ($ class_path) InitPHP: initError ('file'. $ class_name. '. php is not exist! ');
If (! Isset (self: $ instance ['initphp'] [$ class_name]) {
Require_once ($ class_path );
If (! Class_exists ($ class_name) InitPHP: initError ('class'. $ class_name. 'is not exist! ');
$ Init_class = new $ class_name;
Self: $ instance ['initphp'] [$ class_name] = $ init_class;
}
Return self: $ instance ['initphp'] [$ class_name];
}
Author: initphp
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