0

I'm trying to get a list of all the constants that are defined outside of a PHP class but only if they start with a specific prefix (see this tutorial) and saw the ReflectionClass::getConstants functionality but this requires a class.

How do I achieve the same functionality if the constants are defined outside of a class?

2
  • 2
    Maybe try get_defined_constants? Commented May 9, 2019 at 21:14
  • Thanks! An obvious solution eluded me.. I should hand my developer membership card now. Commented May 9, 2019 at 21:15

1 Answer 1

1

You can use the built-in function get_defined_constants. It will list all the constants even the ones coming from Core PHP and loaded extensions. You can pass true as the parameter to categorize them. To get just userland constants (both from const and from define()) use get_defined_constants(true)['user']

<?php

define('DEFINED_CONST', 'foo');
const myConst = 1;

print_r(get_defined_constants(true)['user']);

prints:

Array
(
    [DEFINED_CONST] => foo
    [myConst] => 1
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.