note 69866 added to language.oop5.constants

From: Date: Sun, 24 Sep 2006 11:57:43 +0000
Subject: note 69866 added to language.oop5.constants
Groups: php.notes 
Request: Send a blank email to php-notes+get-117506@lists.php.net to get a copy of this message
Since constants of a child class are not accessible from the parent class via self::CONST and there is no special keyword to access the constant (like this::CONST), i use private static variables and these two methods to make them read-only accessible from object's parent/child classes as well as statically from outside: <?php class b extends a { private static $CONST = 'any value'; public static function getConstFromOutside($const) { return self::$$const; } protected function getConst($const) { return self::$$const; } } ?> With those methods in the child class, you are now able to read the variables from the parent or child class: <?php class a { private function readConst() { return $this->getConst('CONST'); } abstract public static function getConstFromOutside($const); abstract protected function getConst($const); } ?> From outside of the object: <?php echo b::getConstFromOutside('CONST'); ?> You maybe want to put the methods into an interface. However, class b's attribute $CONST is not a constant, so it is changeable by methods inside of class b, but it works for me and in my opinion, it is better than using real constants and accessing them by calling with eval: <?php protected function getConst($const) { eval('$value = '.get_class($this).'::'.$const.';'); return $value; } ?> ---- Server IP: 66.163.161.117 Probable Submitter: 85.181.46.46 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php Edit -- https://master.php.net/note/edit/69866 Del: integrated -- https://master.php.net/note/delete/69866/integrated Del: useless -- https://master.php.net/note/delete/69866/useless Del: bad code -- https://master.php.net/note/delete/69866/bad+code Del: spam -- https://master.php.net/note/delete/69866/spam Del: non-english -- https://master.php.net/note/delete/69866/non-english Del: in docs -- https://master.php.net/note/delete/69866/in+docs Del: other reasons-- https://master.php.net/note/delete/69866 Reject -- https://master.php.net/note/reject/69866 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#117506) next »