note 70337 added to language.oop5.static

From: Date: Thu, 12 Oct 2006 11:36:35 +0000
Subject: note 70337 added to language.oop5.static
Groups: php.notes 
Request: Send a blank email to php-notes+get-118443@lists.php.net to get a copy of this message
One way to get around the fact that static variables are shared throughout the inheritance tree to map a instance variable to a global variable named after the class. <?php $GLOBALS['static'] = array(); class root { public $self; public static $all_instances; function __construct() { $this->self = &$GLOBALS['static'][get_class($this)]; $this->self['instances'] += 1; self::$all_instances += 1; } } class child_a extends root { } class child_b extends root { } $a1 = new child_a(); $a2 = new child_a(); $a3 = new child_a(); $b1 = new child_b(); $b2 = new child_b(); echo "child_a instances: " . $a3->self['instances'] . " | all_instances: " . child_a::$all_instances . "\n"; echo "child_b instances: " . $b2->self['instances'] . " | all_instances: " . child_b::$all_instances . "\n"; echo "\$GLOBALS['static'] = "; var_dump($GLOBALS['static']); ?> // Output: child_a instances: 3 | all_instances: 5 child_b instances: 2 | all_instances: 5 $GLOBALS['static'] = array(2) { ["child_a"]=> &array(1) { ["instances"]=> int(3) } ["child_b"]=> &array(1) { ["instances"]=> int(2) } } ---- Server IP: 66.163.161.117 Probable Submitter: 64.236.227.6 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.static.php Edit -- https://master.php.net/note/edit/70337 Del: integrated -- https://master.php.net/note/delete/70337/integrated Del: useless -- https://master.php.net/note/delete/70337/useless Del: bad code -- https://master.php.net/note/delete/70337/bad+code Del: spam -- https://master.php.net/note/delete/70337/spam Del: non-english -- https://master.php.net/note/delete/70337/non-english Del: in docs -- https://master.php.net/note/delete/70337/in+docs Del: other reasons-- https://master.php.net/note/delete/70337 Reject -- https://master.php.net/note/reject/70337 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118443) next »