Static Constant¶
Static constants are constants that are defined inside a class, interface, trait (since PHP 8.2) and enumeration.
Static constants have a visibility. Visibility may be public, protected, private. By default, and for backward compatibility, a constant without a visibility is public.
Static constants may also be overwritten by children classes. They can also use the final keyword, to avoid such behavior.
Static constants are defined at coding time, and cannot be changed later, not dynamically created.
Static constant syntax is the same than for enumeration cases.
<?php
class X {
public const FOO = 1;
private const BAR = 2;
private const string TYPE = 'valid';
}
?>
See also Everything You Should Know About PHP Const in Class, Class Constants and PHP 8.3 Typed Constants in Production.
Related : Visibility, Final Keyword, Constants In Trait, Constants, Dynamic Constant, Special Constant, Class Constant Syntax, define(), Literal, New In Initializers, Anonymous Constant, Constant Case, Dynamic Class Constant, Typed Constant, Name Conventions