Skip to content

Core\Reflect\ClassInfo::constants

$classInfo->constants(): array<Core\Reflect\ConstantInfo>

Lists the constants of a class.

constants() returns an array with one Core\Reflect\ConstantInfo for each constant of the class. The class's own constants come first. Then come the constants it inherits from parent classes and interfaces. When a subclass declares a constant again, the list has it once, with the subclass's declaration.

Each item has the constant's name and says whether it is public. It also says whether the constant has a value that constant can return. An array constant has no such value. Private and protected constants are in the list too. The list does not contain the values: use constant($name) to read one.

The examples below show the list for one class, the list for a subclass, and a report that prints every public limit of a class with its value.

Return value

Returns array<Core\Reflect\ConstantInfo>. One Core\Reflect\ConstantInfo for each constant of the class, its own declarations first. A constant that a subclass declares again appears once, with the declaration the class uses. The values are not in the list. Use constant to read a value, which checks whether this code may read it.

Changelog

VersionChange
0.0.6The server now builds the description of each class once, when it loads your program. Core\Reflect::forClass, Core\Reflect::forObject and Core\Reflect\EnumInfo::of return that description, so two calls for the same class return the same object. A list such as properties() or methods() is built once per request.

Examples

List the constants of a class

<?nvs
// Shows how to list the constants of a class, with what each one allows.
class Upload
{
public const int MAX_MB = 20;
private const string BUCKET = 'uploads';
public const array<string> TYPES = ['png', 'jpg'];
}
var $info = Core\Reflect::forObject(new Upload());
// `isPublic` is `false` for the private constant `BUCKET`.
// `hasValue` is `false` for `TYPES`. An array constant has no value to return.
foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) {
echo $constant->name(), ': ',
$constant->isPublic() ? 'public' : 'private', ', ',
$constant->hasValue() ? 'has a value' : 'no value', "\n";
}
Output
MAX_MB: public, has a value
BUCKET: private, has a value
TYPES: public, no value

Constants a subclass inherits

<?nvs
// Shows which constants a subclass has: its own, then the ones it inherits.
interface HasVersion
{
public const int VERSION = 2;
}
class Plan implements HasVersion
{
public const int USERS = 5;
public const string SUPPORT = 'email';
}
class TeamPlan extends Plan
{
public const int USERS = 50;
}
// `TeamPlan` declares `USERS` again, so `USERS` is in the list once.
// This prints "USERS", "SUPPORT" and "VERSION".
foreach (Core\Reflect::forObject(new TeamPlan())->constants() as Core\Reflect\ConstantInfo $constant) {
echo $constant->name(), "\n";
}
// The value of `USERS` is the one `TeamPlan` declares. This prints 50.
echo Core\Reflect::forObject(new TeamPlan())->constant('USERS') as int, "\n";
Output
USERS
SUPPORT
VERSION
50

Report every limit

<?nvs
// Shows an admin page that prints every public limit of a service.
// A new limit in the class appears on the page with no other change.
class ApiLimits
{
public const int REQUESTS_PER_MINUTE = 600;
public const int MAX_BODY_KB = 512;
public const bool ALLOW_BATCH = true;
private const string SIGNING_KEY = 'internal';
}
class LimitReport
{
// Prints one line for each public constant that has a value.
public static function show(Core\Reflect\ClassInfo $info): void
{
foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) {
if (!$constant->isPublic() || !$constant->hasValue()) {
continue;
}
// `constant` returns a `mixed` value, and `Core\Json::encode` prints any of them.
echo $constant->name(), ' = ', Core\Json::encode($info->constant($constant->name())), "\n";
}
}
}
// The private `SIGNING_KEY` is not printed.
LimitReport::show(Core\Reflect::forObject(new ApiLimits()));
Output
REQUESTS_PER_MINUTE = 600
MAX_BODY_KB = 512
ALLOW_BATCH = true