note 102199 added to language.oop5.constants
| From: | dexen dot devries at gmail dot com | Date: | Wed, 02 Feb 2011 09:42:53 +0000 |
| Subject: | note 102199 added to language.oop5.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176588@lists.php.net to get a copy of this message | ||
Summary: use ReflectionObject to access class const in PHP older than 5.3.
In versions <= 5.2 you can't use the $object::constMember syntax. Accessing via class name
or the `self' operator doesn't resolve the right class in case of inheritance (for the C++
folks, it behaves like a NON-virtual method). The only working (if ugly) solution to access seems to
be to use the Reflection extension:
<?php
class Foo {
const a = 7;
const x = 99;
}
class Bar extends Foo {
const a = 42; /* overrides the `a = 7' in base class */
}
$b = new Bar();
$r = new ReflectionObject($b);
echo $r->getConstant('a'); # prints `42' from the Bar class
echo "\n";
echo $r->getConstant('x'); # prints `99' inherited from the Foo class
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.228.5.182
----
Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php
Edit -- https://master.php.net/note/edit/102199
Del: integrated -- https://master.php.net/note/delete/102199/integrated
Del: useless -- https://master.php.net/note/delete/102199/useless
Del: bad code -- https://master.php.net/note/delete/102199/bad+code
Del: spam -- https://master.php.net/note/delete/102199/spam
Del: non-english -- https://master.php.net/note/delete/102199/non-english
Del: in docs -- https://master.php.net/note/delete/102199/in+docs
Del: other reasons-- https://master.php.net/note/delete/102199
Reject -- https://master.php.net/note/reject/102199
Search -- https://master.php.net/manage/user-notes.php