Abstract constants in PHP - Force a child class to define a constant

Abstract constants in PHP - Force a child class to define a constant

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Abstract constants in PHP - Force a child class to define a constant

PHP does not support abstract constants. You cannot declare a constant in an abstract method or property, nor can you use the abstract keyword on a const. This limitation stems from how constants are resolved at compile time versus how inheritance and polymorphism function in the language. When you attempt to define a constant in an interface or abstract class, the language expects that specific value to be present immediately upon loading that file. Since child classes may define different values for that constant, a shared definition in the parent becomes logically impossible from the start.

However, this limitation rarely stops developers who need to enforce type safety or configuration consistency across a hierarchy of classes. If you require a child class to define a specific constant before you can use it within an internal method of an abstract class, you must shift your design pattern slightly. The most robust approach involves moving the constant definition from the parent class into the child class itself, while ensuring the parent validates its existence and potentially redefines it if necessary.

Consider a scenario where you are building an abstract repository that requires each driver to define a specific TIMEOUT constant. You might initially try:

abstract class AbstractDriver {
    const TIMEOUT = 0; // This is allowed but doesn't enforce the child's value
    
    public function execute() {
        // Logic using $this::TIMEOUT
    }
}

This works, but it fails to force the child class to define its own constant. To achieve that enforcement, you can leverage the class_exists() check or rely on reflection within the abstract method. A common pattern is to attempt to read the constant inside the method and handle cases where it hasn't been defined yet:

abstract class AbstractDriver {
    public function execute() {
        $timeout = \define('TIMEOUT', 30); // Default fallback
        
        // Check if child redefined it (this is tricky in PHP)
        if (defined('TIMEOUT')) {
            $timeout = \constant('TIMEOUT');
        }
        
        // Use $timeout
    }
}

This approach has a significant flaw: calling define() inside the method means you are setting a global constant, which can lead to unexpected behavior if multiple instances of the abstract class exist in different namespaces or if other parts of your application rely on that constant. A better strategy is to use static properties or traits to manage state, but constants remain immutable by design.

If strict enforcement is required, you might need to switch from const to a public static property with a getter method. This allows the child class to initialize the value in its constructor or via a setter, while the abstract class accesses it through a protected method:

abstract class AbstractDriver {
    protected static $timeout;

    public function execute() {
        if (self::$timeout === null) {
            throw new LogicException('Child class must define timeout.');
        }
        // Execute logic
    }
}

class FileDriver extends AbstractDriver {
    public function __construct() {
        self::$timeout = 30; 
    }
}

This pattern sacrifices the compile-time guarantee of const but offers flexibility and avoids global namespace pollution. It is a standard trade-off when working within PHP's current constraints. If you are using Laravel, this pattern aligns well with service provider registration where you might dynamically configure behavior per driver without relying on rigid abstract constants.

Ultimately, while you cannot force a child class to define a constant in the traditional sense, rethinking the data structure from an immutable constant to a protected static property provides a clean, enforceable solution that respects PHP's compilation model.

Our network