note 96402 added to language.oop5.static

From: Date: Thu, 25 Feb 2010 09:38:46 +0000
Subject: note 96402 added to language.oop5.static
Groups: php.notes 
Request: Send a blank email to php-notes+get-166347@lists.php.net to get a copy of this message
On PHP 5.2.x or previous you might run into problems initializing static variables in subclasses due to the lack of late static binding: <?php class A { protected static $a; public static function init($value) { self::$a = $value; } public static function getA() { return self::$a; } } class B extends A { protected static $a; // redefine $a for own use // inherit the init() method public static function getA() { return self::$a; } } B::init('lala'); echo 'A::$a = '.A::getA().'; B::$a = '.B::getA(); ?> This will output: A::$a = lala; B::$a = If the init() method looks the same for (almost) all subclasses there should be no need to implement init() in every subclass and by that producing redundant code. Solution 1: Turn everything into non-static. BUT: This would produce redundant data on every object of the class. Solution 2: Turn static $a on class A into an array, use classnames of subclasses as indeces. By doing so you also don't have to redefine $a for the subclasses and the superclass' $a can be private. Short example on a DataRecord class without error checking: <?php abstract class DataRecord { private static $db; // MySQLi-Connection, same for all subclasses private static $table = array(); // Array of tables for subclasses public static function init($classname, $table, $db = false) { if (!($db === false)) self::$db = $db; self::$table[$classname] = $table; } public static function getDB() { return self::$db; } public static function getTable($classname) { return self::$table[$classname]; } } class UserDataRecord extends DataRecord { public static function fetchFromDB() { $result = parent::getDB()->query('select * from '.parent::getTable('UserDataRecord').';'); // and so on ... return $result; // An array of UserDataRecord objects } } $db = new MySQLi(...); UserDataRecord::init('UserDataRecord', 'users', $db); $users = UserDataRecord::fetchFromDB(); ?> I hope this helps some people who need to operate on PHP 5.2.x servers for some reason. Late static binding, of course, makes this workaround obsolete. ---- Server IP: 85.214.154.209 Probable Submitter: 134.176.192.174 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.static.php Edit -- https://master.php.net/note/edit/96402 Del: integrated -- https://master.php.net/note/delete/96402/integrated Del: useless -- https://master.php.net/note/delete/96402/useless Del: bad code -- https://master.php.net/note/delete/96402/bad+code Del: spam -- https://master.php.net/note/delete/96402/spam Del: non-english -- https://master.php.net/note/delete/96402/non-english Del: in docs -- https://master.php.net/note/delete/96402/in+docs Del: other reasons-- https://master.php.net/note/delete/96402 Reject -- https://master.php.net/note/reject/96402 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166347) next »