I am wanting to use reflection to obtain a list of the constants defined by a class in PHP.
Currently using reflection I can get a list of the constants, but this also includes the ones declared in inherited classes. Is there a method I can use to either;
- Given a class, get the constants defined by that class only
- Given a constant and a class, check if that constant was defined by that class (not an inherited or extended parent).
For example, in the following code:
class Foo {
const PARENT_CONST = 'parent';
const ANOTHER_PARENT_CONST = 'another_parent';
}
class Bar extends Foo {
const CHILD_CONST = 'child';
const BAR_CONST = 'bar_const';
}
$reflection = new ReflectionClass('Bar');
print_r($reflection->getConstants());
The output is:
Array
(
[CHILD_CONST] => child
[BAR_CONST] => bar_const
[PARENT_CONST] => parent
[ANOTHER_PARENT_CONST] => another_parent
)
But I would like to have only this:
Array
(
[CHILD_CONST] => child
[BAR_CONST] => bar_const
)