static::class cannot be used for compile-time class name resolution
Description
static::class resolves to the class name of whichever subclass is actually in scope at runtime, through late static binding. A class constant, on the other hand, is resolved once and shared identically by the declaring class and every class that inherits it, so it cannot hold a value that is meant to vary per subclass. self::class is accepted in the same spot because it never varies.
Example
<?php
class Base {
const NAME = static::class;
}
?>
Alternatives
- Use self::class if a fixed class name is acceptable.
- Expose it through a method instead: public static function name(): string { return static::class; }.
Related error messages
- static::-“-is-not-allowed-in-compile-time-constants
- static-“-is-not-allowed-in-compile-time-constants
Changed Behavior
This error may appear following an evolution in behavior, in previous versions. See