0

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!

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.