note 54843 modified in language.oop5.patterns by danbrown

From: Date: Sun, 23 Jan 2011 16:46:37 +0000
Subject: note 54843 modified in language.oop5.patterns by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176122@lists.php.net to get a copy of this message
If you need singleton of any class and don't want write singleton wrapper for each this class, try this: <?php class UniverzalSingleton { const EXCEPTION_NO_CLASS = 1; protected static $_instances = array(); public static function getInstance($class) { if (!isset(self::$_instances[$class])) { if (!class_exists($class)) throw (new Exception(__CLASS__ . ': Requsted class is not exist.', self::EXCEPTION_NO_CLASS)); self::$_instances[$class] = new $class(); } // if return self::$_instances[$class]; } // getInstance } // class UniverzalSingleton ?> --was-- If you need singleton of any class and don't want write singleton wrapper for each this class, try this: class UniverzalSingleton { const EXCEPTION_NO_CLASS = 1; protected static $_instances = array(); public static function getInstance($class) { if (!isset(self::$_instances[$class])) { if (!class_exists($class)) throw (new Exception(__CLASS__ . ': Requsted class is not exist.', self::EXCEPTION_NO_CLASS)); self::$_instances[$class] = new $class(); } // if return self::$_instances[$class]; } // getInstance } // class UniverzalSingleton http://php.net/manual/en/language.oop5.patterns.php

« previous php.notes (#176122) next »