Skip to content
Daily Dev Tip

This tip lives in your new tab — for free.

Install for Chrome →
PHP

Type your class constants in PHP 8.3 to lock the contract across subclasses

app/Contracts/HasVersion.php
php
interface HasVersion
{
    const string VERSION = '1.0';
}

class Api implements HasVersion
{
    // Fatal error if redeclared with a non-string type
    const string VERSION = '2.1';
}

PHP 8.3 added type declarations for class, interface, and enum constants. Without a type, a subclass could override a constant with an incompatible value and nothing would complain until it broke at runtime. Typing the constant enforces the contract at declaration time, which matters most on interfaces and base classes where implementers are expected to override a constant but keep its shape.

php8.3constantstypesinterfaces