Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP class constants

Reference for PHP class constants: const syntax, visibility, final constants, typed constants (PHP 8.3), ::class, enum cases and constant expressions.

A class constant is a named value that belongs to a class and cannot change. You declare it with const inside the class body and read it with ClassName::NAME, self::NAME or static::NAME. No object is needed.

Class constants may have a visibility modifier, can be marked final, and since PHP 8.3 can have a type. They are inherited by subclasses and can be overridden unless they are final.

PHP
class Http
{
    public const OK = 200;
    public const NOT_FOUND = 404;
}
echo Http::NOT_FOUND;   // 404

Try it

PHP Declaring and reading constants

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP final, typed constants and inheritance

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
[final] [visibility] const [type] NAME = constant-expression [, OTHER = ...];
Part Description
Visibility public (default), protected or private.
final PHP 8.1+. Subclasses cannot override the constant.
type PHP 8.3+. For example const string NAME = 'x'. The value must match the type.
Value A constant expression: literals, other constants, operators, new in some places (8.1+), enum cases, arrays.

Access forms: Class::NAME, $object::NAME, self::NAME, static::NAME, parent::NAME, constant('Class::NAME'), and Class::class.

Errors: reading an undefined constant throws Error ("Undefined constant"); reading a private constant from outside throws Error.

Usage notes

  • Constants are not variables. There is no $ and they cannot be reassigned.
  • Enum cases are constants too. Use enums instead of groups of related integer or string constants when you want type safety.
  • Interface constants are inherited by implementing classes. Before PHP 8.1 they could not be overridden; now they can unless final.
  • static::NAME gives late static binding: it picks the constant from the class that was actually called.
  • Name::class is resolved at compile time and does not load the class, so it is safe for classes that do not exist yet.
  • Constant arrays are allowed and are immutable: const ROLES = ['admin', 'user'];.
  • Expressions in constants are evaluated once, on first use.
  • Traits can declare constants since PHP 8.2.
  • Compared with define(): class constants are scoped to a class; const at file top level and define() create global constants. See const and define.
  • Reflection: ReflectionClassConstant gives attributes, visibility and the declaring class.