note 70025 added to language.oop5.patterns

From: Date: Sat, 30 Sep 2006 00:38:08 +0000
Subject: note 70025 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-117874@lists.php.net to get a copy of this message
cortex is right - it took me hours to figure it out. None the less it's sometimes useful to let one sigleton class extend another. You can achieve this by only using static attributes: <?php class A { private static $value = 0; private static $instance = null; private function __construct() { $this->set(time()); } public static function getInstance() { if (is_null((self::$instance))) { $class_name = __CLASS__; self::$instance = new $class_name; } return self::$instance; } private function set($i) { self::$value = $i; $this->out(); } public function out() { echo self::$value; } } class B extends A { public static $instance = null; public static function getInstance() { parent::getInstance(); if (is_null(self::$instance)) { $class = __CLASS__; self::$instance = new $class; } return self::$instance; } } $b = B::getInstance(); $b->out(); ?> This will output the current time twice. If $value isn't static, the call to $b->out() will output 0. Maybe it can save someone some time... ---- Server IP: 217.13.201.10 Probable Submitter: 89.53.219.149 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/70025 Del: integrated -- https://master.php.net/note/delete/70025/integrated Del: useless -- https://master.php.net/note/delete/70025/useless Del: bad code -- https://master.php.net/note/delete/70025/bad+code Del: spam -- https://master.php.net/note/delete/70025/spam Del: non-english -- https://master.php.net/note/delete/70025/non-english Del: in docs -- https://master.php.net/note/delete/70025/in+docs Del: other reasons-- https://master.php.net/note/delete/70025 Reject -- https://master.php.net/note/reject/70025 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117874) next »