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

Solutions

  • Use the trait in a class and access its features.

  • Convert the trait into a class and access its features.