Pattern: Singleton
4 Апрель 2009
Шаблон проектирования Singleton (одиночка) используется для того чтобы класс можно было создать только один раз. На практике он применяется вместе с другим шаблонами проектирования такими как pattern Registry и pattern ServiceLocator
class Singleton {
private static $instance; // object
/**
* Private constructor does nothing
*
*/
final private function __construct(){
/* ... */
}
/**
* Return the single instance of object
*
* @return object
*/
public static function & __instance(){
if (!isset(self :: $instance))
self :: $instance = new self;
return self :: $instance;
}
}
$single = Singleton::__instance();
Свежие комментарии