I'm having a class with a list of constants. I want something like get_object_vars to dump constants into array. What is the simpliest way to do that?
Thank you in advance!
This will require use of the Reflection class:
function getClassConstants($class) {
$reflection = new ReflectionClass(
is_object($class) ? get_class($class) : $class
);
return $reflection->getConstants();
}
// usage: $constants = getClassConstants('myClass');
// $constants = getClassConstants($myClassObjectInstance);
or you can implement it as a method in your class by passing it $this instead of an argument
Documentation
PHP's Reflection class- https://www.php.net/manual/en/class.reflectionclass.php
PHP's ReflectionClass::getConstants - https://www.php.net/manual/en/reflectionclass.getconstants.php