Menu

← Back to classConstant.*

Error Identifier: classConstant.onTrait

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

trait MyTrait
{
	public const FOO = 'bar';
}

echo MyTrait::FOO;

Why is it reported? #

Accessing a constant directly on a trait name is not allowed. While traits can define constants (since PHP 8.2), those constants can only be accessed through a class that uses the trait, not through the trait itself. PHP does not support accessing trait constants via TraitName::CONSTANT.

In the example above, MyTrait::FOO attempts to access the constant FOO directly on the trait MyTrait.

How to fix it #

Access the constant through a class that uses the trait:

 <?php declare(strict_types = 1);
 
 trait MyTrait
 {
 	public const FOO = 'bar';
 }

 class MyClass
 {
 	use MyTrait;
 }

-echo MyTrait::FOO;
+echo MyClass::FOO;

How to ignore this error #

You can use the identifier classConstant.onTrait to ignore this error using a comment:

// @phpstan-ignore classConstant.onTrait
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: classConstant.onTrait

Rules that report this error #

  • PHPStan\Rules\Classes\ClassConstantRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.