at 2008-08-23
in PHP5
by friebe
(0 comments)
With the so-called "late static binding" feature in PHP 5.3 and the function get_called_class() at hand we can realize a singleton base class. This will alleviate creating singleton classes -the only thing needed is to extend the base class:
class Logger extends Singleton { public function log($arg) { } } Logger::getInstance()->log('Hello');Base class implementation
abstract class Singleton { private static $instances= array(); private final function __construct() { } private final function __clone() { } public static function getInstance() { $id= get_called_class(); if (!isset(self::$instances[$id])) { self::$instances[$id]= new static(); } return self::$instances[$id]; } } Critique Granted, from a "is-a" point of view, it would be nicer to use mixins for this kind of thing. From a maintenance point of view, this might be worth considering, though.
|
Subscribe
You can subscribe to the XP framework's news by using RSS syndication.
CategoriesNews General PHP5 Announcements RFCs Further reading Examples Editorial EASC Experiments Unittests Databases 5.8-SERIES Unicode Language 5.9-SERIES
RelatedFind related articles by a search for «Singleton».
|