Skip to content

MIR0012

TraitConstantAccessedDirectly

A trait constant is accessed directly (e.g., MyTrait::CONST_NAME). In PHP, trait constants cannot be accessed directly; they must be accessed through a class that uses the trait.

<?php
trait MyTrait {
const NAME = 'trait';
}
class MyClass {
use MyTrait;
}
// Error: accessing trait constant directly
echo MyTrait::NAME;

Access the constant through a class that uses the trait:

<?php
trait MyTrait {
const NAME = 'trait';
}
class MyClass {
use MyTrait;
}
// Correct: access through the class
echo MyClass::NAME;