Static Constant
Static constants are constants that are defined inside a class, an interface, a trait since PHP 8.2, or an enumeration; depending on the containing structure, they are also called an interface constant, a trait constant, or an enum constant.
Static constants have a visibility. Visibility may be public, protected, or private. By default, and for backward compatibility, a constant without a visibility is public.
Static constants may also be overwritten by child classes. They can also use the final keyword, to avoid such behavior.
Static constants are defined at coding time, and cannot be changed later, nor dynamically created.
Static constant syntax is the same as for enumeration cases.
<?php
class Elephpant {
public const HABITAT = 1;
private const WEIGHT = 2;
private const string TYPE = 'valid';
}
?>
See Also
- Everything You Should Know About PHP Const in Class
- Class Constants
- PHP 8.3 Typed Constants in Production