PHP OOP - Class Constants
In PHP Object-Oriented Programming (OOP), class constants are immutable values defined within a class using the const
keyword. They are useful for defining values that should not change and are specific to the class's context.
Syntax:
Accessing Class Constants:
Class constants are accessed using the scope resolution operator ::
:
You can also access them from within the class using self::
:
Inheritance with Constants:
Child classes can inherit constants, but they cannot override them directly:
If a child defines a constant with the same name, it doesn't override the parent — it simply defines a new constant for the child class.