Skip to content

Constants

A constant is a value that never changes. Every constant belongs to a class. There are no global constants.

Class constants and ::class

A class can hold named values that never change, and every class knows its own name.

A constant is written public const int MAX = 3;, with its type. Code inside the class reads it as self::MAX, and code anywhere else as Limits::MAX. The value must be written directly in the code, as one value or a list.

A subclass can give a constant a new value. In a method, static::MAX reads the value of the class the call was made on. self::MAX reads the value of the class the code is written in. static:: works only for a single value, not inside an anonymous function.

Limits::class gives the name of a class as a string. $order::class gives the class a value really is.

A constant list gives you a new list on every read.

The examples below show self:: and static::, class names as text, a constant list, and static:: with a list, which does not compile.

Values that never change

<?nvs
// Shows a class holding values that never change. Every constant writes its
// type, and code inside the class reads it with `self::` or `static::`.
class Upload
{
public const uint MAX_BYTES = 1048576;
public const string FOLDER = 'uploads';
// The element type is `float`, so this constant holds 1.0 and 2.5.
public const array<float> STEP_SECONDS = [1, 2.5];
// Returns true when a file of this size may be stored. `static::MAX_BYTES`
// reads the constant of the class the call was made on.
public static function fits(uint $bytes): bool
{
return $bytes <= static::MAX_BYTES;
}
// `self::MAX_BYTES` always reads the value written in this class.
public static function describe(): string
{
return static::MAX_BYTES as string . ' of ' . self::MAX_BYTES as string . ' bytes';
}
}
// A subclass gives the constant a smaller value.
class AvatarUpload extends Upload
{
public const uint MAX_BYTES = 65536;
}
// From outside the class, the name of the class comes first. This prints
// 1048576 and then uploads.
echo Upload::MAX_BYTES, "\n";
echo Upload::FOLDER, "\n";
// `fits` uses `static::`, so each class checks against its own limit.
if (Upload::fits(500000)) {
echo "500000 bytes fit an upload\n";
}
if (!AvatarUpload::fits(500000)) {
echo "500000 bytes do not fit an avatar\n";
}
// This prints `65536 of 1048576 bytes`. The first number comes from
// `static::`, the second from `self::`.
echo AvatarUpload::describe(), "\n";
// The first value is written as a whole number. The declared type makes it a
// float, so this division prints 0.5.
echo Upload::STEP_SECONDS[0] / 2, "\n";
Output
1048576
uploads
500000 bytes fit an upload
500000 bytes do not fit an avatar
65536 of 1048576 bytes
0.5

The name of a class as text

<?nvs
// Shows how to get the name of a class as a string. On a value, `::class`
// gives the class that value really is.
class Notification
{
public static function label(): string
{
// `static::class` is the class the call was made on. It is not always
// the class this method is written in.
return static::class;
}
}
class EmailNotification extends Notification
{}
class SmsNotification extends Notification
{}
// `Class::class` is the name of that class, written as text. This prints
// Notification.
echo Notification::class, "\n";
// The call decides what `static::class` gives. This prints EmailNotification
// and then SmsNotification.
echo EmailNotification::label(), "\n";
echo SmsNotification::label(), "\n";
// A variable declared as the parent class can hold a subclass. `::class` gives
// the class the value was created from, so this prints SmsNotification.
Notification $sent = new SmsNotification();
echo $sent::class, "\n";
Output
Notification
EmailNotification
SmsNotification
SmsNotification

A constant list as an allow list

<?nvs
// Shows a constant list checked on the way in, which is what a service does
// with a small fixed table of allowed values.
class Images
{
public const array<string> TYPES = ['image/png', 'image/jpeg', 'image/webp'];
// Returns true when this service stores files of that type.
public static function allowed(string $type): bool
{
return Core\Arr::contains(self::TYPES, $type);
}
}
array<string> $uploads = ['image/png', 'application/pdf', 'image/webp'];
foreach ($uploads as string $type) {
if (Images::allowed($type)) {
echo $type, " is stored\n";
} else {
echo $type, " is rejected\n";
}
}
// Every read of the constant gives a new list. This one adds a type to the
// list it got, and the constant still holds three types, so this prints 4 3.
array<string> $mine = Images::TYPES;
$mine[] = 'image/gif';
echo Core\Arr::count($mine), ' ', Core\Arr::count(Images::TYPES), "\n";
Output
image/png is stored
application/pdf is rejected
image/webp is stored
4 3

Static and an array constant

<?nvs
// Shows that `static::` cannot read an array constant, so this program does
// not compile. `self::STEPS` or `Retry::STEPS` reads it.
// proof: compile-error
class Retry
{
public const array<int> STEPS = [1, 2, 4];
public static function count(): int
{
return Core\Arr::count(static::STEPS);
}
}
Output
error[E0832]: `static::STEPS` reads `Retry::STEPS`, which is `array<int>` and has no scalar value
--> 04-static-and-an-array-constant.nvs:12:32
|
12 | return Core\Arr::count(static::STEPS);
| ^^^^^^^^^^^^^ read through `static::`
= help: a `static::` constant is read when the program runs, and only a `string`, `int`, `uint`, `bool` or `float` constant can be read that way. Write `self::` or the class name, or make the constant one of those types
error: aborting due to 1 error
To read what a code means and how to fix it, run `nvs agent show <code>`.

Details

A constant name is SCREAMING_SNAKE_CASE, so const int max is the error E0113. static on a constant is the error E0106. An unknown constant is E0309. A constant cannot be assigned: A::N = 2; is the error E0105, and A::L[] = 2; is E0700. A constant is read through the class name, so $a::N is the error E0496 and $a->N is E0405.

A subclass that declares the same constant again keeps its type, or a narrower one. An int|string constant may become int. A string in place of an int is the error E0833. An interface may declare a constant, and a class that implements it may declare it again. In a default method, static::NAME reads the value of the class, or the interface's value when the class has none.

static::NAME works for string, int, uint, bool and float constants. For an array or a ?int constant it is the error E0832. A parameter default cannot use static:: (E0831), and self::NAME works there.

Class::class, self::class and parent::class are fixed when the program compiles. static::class and $obj::class are read when the program runs. The result is a string, so int $n = A::class; is the error E0401. $v::class on a mixed, a ?A or an anonymous object is the error E0702. Check the value with is first. On a class<A> value it is E0702 too. Write $c as string to get the name.

foreach works on an array constant like on any other array.

  • Do use a constant for a fixed limit or a fixed label, such as MAX_ITEMS.
  • Do write constant names in SCREAMING_SNAKE_CASE.
  • Don’t use a constant for a value that comes from configuration. Read it when the program runs.