Cannot use trait %s
Description
An enum’s cases and its constants share a single namespace: a case and a constant cannot have the same name, whether the constant is declared directly in the enum or brought in through a trait.
Here, the trait HasColor declares a constant called Red, and the enum also declares a case called Red; composing the enum with the trait would leave two different things answering to the same name, so PHP refuses to use the trait.
This entry records only the first sentence of the actual engine message, which continues with , because %s::%s conflicts with enum case %s::%s, naming the trait, the constant, the enum and the case involved.
Example
<?php
trait HasColor {
const Red = 'red';
}
enum Color {
use HasColor;
case Red;
}
?>
Literal Examples
- Cannot use trait HasColor, because HasColor::Red conflicts with enum case Color::Red
Alternatives
- Rename the constant in the trait so it no longer collides with the enum case.
- Rename the conflicting case in the enum.
- Stop using the trait in this enum, and declare the constant directly in the enum, merging it with the case some other way.
Related error messages
Changed Behavior
This error may appear following an evolution in behavior, in previous versions. See