Cannot access trait constant %s::%s directly
Description
Traits cannot be used indepently from a host class. They must be used inside a class, with the use keyword.
It is also not possible to call a method or a property on a trait.
Trait constants were introduced after direct access to trait features was banned, so there was no time where accessing a trait constant was possible, unlike trait properties or methods.
Example
<?php
trait T {
public static const C = 1;
}
echo T::C;
?>
Literal Examples
- Cannot access trait constant T::C directly
Alternatives
- Use the trait in a class and access its features.
- Convert the trait into a class and access its features.
Related error messages
- accessing-static-trait-property-%s::$%s-is-deprecated
- calling-static-trait-method-%s::%s-is-deprecated
- traits-cannot-have-constants
- accessing-static-trait-property-%s::$%s-is-deprecated,-it-should-only-be-accessed-on-a-class-using-the-trait
In previous PHP versions, this error message used to be :ref:traits-cannot-have-constants.