PHP design mode-singleton

Source: Internet
Author: User

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

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.