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.
- For example, a database instance typically goes through a singleton pattern.
- 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