note 86133 added to function.get-class

From: Date: Sat, 04 Oct 2008 15:36:39 +0000
Subject: note 86133 added to function.get-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-145049@lists.php.net to get a copy of this message
The code just worked fine with PHP 5.3. Thanks to you it could be rebuild to work with earlier versions of PHP 5. I just changed the 'static' to 'self' and die() to an Exception. <? // This one extends Singleton with proper class name class someSingleton extends Singleton { public static function getInstance() { parent::$__CLASS__ = __CLASS__; parent::getInstance(); } } // This one does the logic abstract class Singleton { protected static $__CLASS__ = __CLASS__; protected static $instance = null; protected function __construct() {} abstract protected function init(); /** * Gets an instance of this singleton. If no instance exists, a new instance is created and returned. * If one does exist, then the existing instance is returned. */ public static function getInstance() { $class = self::getClass(); if (self::$instance === null) { self::$instance = new $class(); self::$instance->init(); } return self::$instance; } /** * Returns the classname of the child class extending this class * * @return string The class name */ private static function getClass() { $implementing_class = self::$__CLASS__; $original_class = __CLASS__; if ($implementing_class === $original_class) throw new Exception("You MUST provide a <code>protected static \$__CLASS__ = __CLASS__;</code> statement in your Singleton-class!"); return $implementing_class; } } ---- Server IP: 69.147.83.197 Probable Submitter: 91.67.246.14 ---- Manual Page -- http://www.php.net/manual/en/function.get-class.php Edit -- https://master.php.net/note/edit/86133 Del: integrated -- https://master.php.net/note/delete/86133/integrated Del: useless -- https://master.php.net/note/delete/86133/useless Del: bad code -- https://master.php.net/note/delete/86133/bad+code Del: spam -- https://master.php.net/note/delete/86133/spam Del: non-english -- https://master.php.net/note/delete/86133/non-english Del: in docs -- https://master.php.net/note/delete/86133/in+docs Del: other reasons-- https://master.php.net/note/delete/86133 Reject -- https://master.php.net/note/reject/86133 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145049) next »