/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
/** * Singleton pattern implementation */ abstract class Singleton { /** * Collection of instances * @var array */ private static $_aInstance = array(); /** * Private constructor */ private function __construct(){} /** * Get instance of class */ public static function getInstance() { // Get name of current class $sClassName = get_called_class(); // Create new instance if necessary if ( !isset( self::$_aInstance[ $sClassName ] ) ) self::$_aInstance[ $sClassName ] = new $sClassName(); $oInstance = self::$_aInstance[ $sClassName ]; return $oInstance; } /** * Private final clone method */ final private function __clone(){} } * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- class Example extends Singleton {}
- $ oExample1 = Example :: getInstance ();
- $ oExample2 = Example :: getInstance ();
- echo (is_a ($ oExample1, 'Example' ) && $ oExample1 === $ oExample2)
- ? 'Same' : 'Different' , "\ n" ;
Source: https://habr.com/ru/post/85852/
All Articles