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

Fetching properties on non-enums in constant expressions is not allowed

Description

Constant expressions are expressions that build the value of a constant. A subset of the features of PHP are available in these expressions.

In particular, objects are allowed, but it is not possible to fetch properties nor call methods. In fact, properties are allowed, but on a specific type of object: enumeration cases.

Example

<?php

class Y {
	public $y = 1;
}

const Y = new Y();

enum Z : string {
	case Z = 'foo';
}

class X {
	const XZ = Z::z->value;
	const X = Y->y;
}

// OK
echo X::XZ;

//Fetching properties on non-enums in constant expressions is not allowed
echo X::X;

?>

Alternatives

  • Do not use properties in constant expressions.