note 104260 added to language.oop5.constants

From: Date: Sat, 04 Jun 2011 08:52:19 +0000
Subject: note 104260 added to language.oop5.constants
Groups: php.notes 
Request: Send a blank email to php-notes+get-180246@lists.php.net to get a copy of this message
it's possible to declare constant in base class, and override it in child, and access to correct value of the const from the static method is possible by 'get_called_class' method: <?php abstract class dbObject { const TABLE_NAME='undefined'; public static function GetAll() { $c = get_called_class(); return "SELECT * FROM ".$c::TABLE_NAME.""; } } class dbPerson extends dbObject { const TABLE_NAME='persons'; } class dbAdmin extends dbPerson { const TABLE_NAME='admins'; } echo dbPerson::GetAll()."<br>";//output: "SELECT * FROM persons" echo dbAdmin::GetAll()."<br>";//output: "SELECT * FROM admins" ?> ---- Server IP: 69.147.83.197 Probable Submitter: 195.191.146.11 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php Edit -- https://master.php.net/note/edit/104260 Del: integrated -- https://master.php.net/note/delete/104260/integrated Del: useless -- https://master.php.net/note/delete/104260/useless Del: bad code -- https://master.php.net/note/delete/104260/bad+code Del: spam -- https://master.php.net/note/delete/104260/spam Del: non-english -- https://master.php.net/note/delete/104260/non-english Del: in docs -- https://master.php.net/note/delete/104260/in+docs Del: other reasons-- https://master.php.net/note/delete/104260 Reject -- https://master.php.net/note/reject/104260 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#180246) next »