Abstract Constants
By WebSmithery
<?php
abstract class Foo {
// self-referential 'abstract' declaration
const NAME = self::NAME;
}
class Fooling extends Foo {
// Overrides definition from parent class
// Without this declaration, an error will be triggered
const NAME = 'Donald';
}
There’s a pretty common pattern to declare “abstract class constants” in PHP.
PHP lazy loading will prevent the error “Fatal error: Uncaught Error: Cannot declare self-referencing constant self::NAME” if the constant is overloaded.
This makes an effective ‘abstract constant’, that must be defined to be usable.
See Also
- Class Constants (PHP manual)
- Class Abstraction (PHP manual)
- Abstract constants in PHP - Force a child class to define a constant
- Abstract Class Constant on 3v4l.org [Try me]
PHP Features
Last updated: 10 September 2026