Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

PHP Features

Last updated: 10 September 2026