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

Cannot redefine class constant

Description

It is only allowed to defined a constant once per class.

A constant with the same name may be defined in a parent or child class, or even in a trait: there are rules that allow PHP to select one of them, whenever there is a possible name conflict.

At the class, interface, trait level, the constants must all be distinct.

At the enumeration level, the naming conflict may arise between a case and a const: the error message is then the same.

This error applies to class constants, and there is a different error for global constants.

Example

<?php

class X {
	const A = 1, A = 1;
}

enum E {
	case B;

	const B = 1;
}

?>

Alternatives

  • Remove one of the constant.
  • Rename one of the constant.
  • Move one of the constant to a parent, trait or interface.