PHP Design Pattern Series-Single case

Source: Internet
Author: User

    • Single-Case mode

By providing access to a shared instance of itself, the singleton design pattern is used to restrict the creation of a specific object only once.

    • Usage Scenarios
    1. For example, a database instance typically goes through a singleton pattern.
    2. Singleton mode can reduce instantiation of classes
    • Code: Source initphp Framework, first detects that the class has not been instantiated, instantiated the use of an object instance that has already been stored in a static variable, without instantiating and saving the object.
    /** Frame Core loading-all classes of the framework need to go out through the function * 1. Singleton mode * 2. You can load class files in-controller,service,view,dao,util,library * 3. Framework load Core function * Usage: $this->load ($class _name, $type) * @param string $class _name class name * @param string $type class Don't*/       Public functionLoad$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; }          returnSelf::$instance[' initphp '] [$class _name]; }  

Transferred from: http://blog.csdn.net/initphp/article/details/7755765

PHP Design Pattern Series-Single case

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.