Skip to content

Core\Reflect\ConstantInfo::name

$constantInfo->name(): string

Returns the name of a class constant.

name() returns the name of the constant that this Core\Reflect\ConstantInfo describes. The name is written exactly as the class declares it, in upper case like MAX_ITEMS. It has no class name in front of it and no ::. A text that is equal to the name only when case is ignored, such as max_items, is not the name.

Private and protected constants have a name too, and name() returns it. To read the value, pass the name to Core\Reflect\ClassInfo::constant.

The examples below show how to print every constant name of a class, how to find a name that was typed with the wrong case, and how to check the keys of a configuration against the constant names of a class.

Return value

Returns string. The name with no class name and no ::. Core\Reflect\ClassInfo::constant takes this name.

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

Print the constant names

<?nvs
// Shows how to print the name of every constant of a class.
class Order
{
public const int MAX_ITEMS = 50;
public const string CURRENCY = 'EUR';
private const string ID_PREFIX = 'ord_';
}
var $info = Core\Reflect::forObject(new Order());
// `name` returns the name as the class writes it.
// The private constant `ID_PREFIX` is in the list too.
foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) {
echo $constant->name(), "\n";
}
Output
MAX_ITEMS
CURRENCY
ID_PREFIX

Find a name typed with the wrong case

<?nvs
// Shows that constant names are case-sensitive, and how to find the right name for a typing mistake.
class Page
{
public const int PER_PAGE = 20;
public const int MAX_PAGES = 100;
}
var $info = Core\Reflect::forObject(new Page());
string $typed = 'per_page';
// `per_page` and `PER_PAGE` are two different names, so the first loop finds nothing.
foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) {
if ($constant->name() == $typed) {
echo 'exact match: ', $typed, "\n";
}
}
// `Core\Str::lower` makes both names lower case. This prints "did you mean PER_PAGE?".
foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) {
if (Core\Str::lower($constant->name()) == Core\Str::lower($typed)) {
echo 'did you mean ', $constant->name(), "?\n";
}
}
Output
did you mean PER_PAGE?

Check configuration keys

<?nvs
// Shows how to check the keys of a configuration against the constant names of a class.
class Defaults
{
public const int TIMEOUT = 30;
public const int RETRIES = 3;
public const int PORT = 8080;
}
// These keys come from a configuration file. `RETRY` is a typing mistake.
array<string> $keys = ['TIMEOUT', 'RETRY', 'PORT'];
var $info = Core\Reflect::forObject(new Defaults());
// First, collect the name of every constant.
array<string> $known = [];
foreach ($info->constants() as Core\Reflect\ConstantInfo $constant) {
$known[] = $constant->name();
}
// A known key prints its default value. An unknown key prints an error line.
foreach ($keys as string $key) {
if (Core\Arr::contains($known, $key)) {
echo $key, ' defaults to ', $info->constant($key) as int, "\n";
} else {
echo $key, " is not a setting\n";
}
}
Output
TIMEOUT defaults to 30
RETRY is not a setting
PORT defaults to 8080