note 123353 added to reflectionclass.getconstants
| From: | Panni@php.net | Date: | Sat, 17 Nov 2018 21:21:17 +0000 |
| Subject: | note 123353 added to reflectionclass.getconstants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209720@lists.php.net to get a copy of this message | ||
If you want to define a static getConstants() function which works with inheritance you can do the
following:
<?php
abstract class AbstractClass
{
const TEST = "test";
public static function getConstants()
{
// "static::class" here does the magic
$reflectionClass = new ReflectionClass(static::class);
return $reflectionClass->getConstants();
}
}
class ChildClass extends AbstractClass
{
const TYPE_A = 1;
const TYPE_B = 'hello';
}
$example = new ChildClass();
var_dump($example->getConstants());
// Result:
array(3) {
'TYPE_A' => int(1)
'TYPE_B' => string(5) "hello"
'TEST' => string(4) "test"
}
?>
----
Server IP: 208.43.231.9
Probable Submitter: 92.72.144.95
----
Manual Page -- http://php.net/manual/en/reflectionclass.getconstants.php
Edit -- https://master.php.net/note/edit/123353
Del: integrated -- https://master.php.net/note/delete/123353/integrated
Del: useless -- https://master.php.net/note/delete/123353/useless
Del: bad code -- https://master.php.net/note/delete/123353/bad+code
Del: spam -- https://master.php.net/note/delete/123353/spam
Del: non-english -- https://master.php.net/note/delete/123353/non-english
Del: in docs -- https://master.php.net/note/delete/123353/in+docs
Del: other reasons-- https://master.php.net/note/delete/123353
Reject -- https://master.php.net/note/reject/123353
Search -- https://master.php.net/manage/user-notes.php