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

static::" is not allowed in compile-time constants

Description

Class constants are meant to hold a single, fixed value shared identically by the declaring class and every subclass that inherits it. Referencing static:: inside a constant expression would make the value depend on late static binding, i.e. on whichever subclass actually reads it, which contradicts a constant’s fixed nature, so PHP rejects it at compile time.

Example

<?php

class Base {
	const A = 1;
	const B = static::A * 2;
}

?>

Alternatives

  • Use self:: instead if the intent is to always refer to the declaring class’s own member.
  • Compute the value in a static method instead of a constant.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See