Menu

← Back to attribute.*

Error Identifier: attribute.constructorNotPublic

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);

#[\Attribute]
class MyAttribute
{
	private function __construct(public string $name)
	{
	}
}

#[MyAttribute('test')]
class Foo
{
}

Why is it reported? #

PHP requires the constructor of an attribute class to be public. When an attribute is applied, PHP internally instantiates the attribute class, and it can only do so if the constructor is accessible. A non-public constructor (private or protected) will cause a runtime error when the attribute is used.

In the example above, MyAttribute has a private constructor, so using #[MyAttribute('test')] will fail.

How to fix it #

Change the attribute class constructor visibility to public:

 <?php declare(strict_types = 1);
 
 #[\Attribute]
 class MyAttribute
 {
-	private function __construct(public string $name)
+	public function __construct(public string $name)
 	{
 	}
 }

 #[MyAttribute('test')]
 class Foo
 {
 }

How to ignore this error #

You can use the identifier attribute.constructorNotPublic to ignore this error using a comment:

// @phpstan-ignore attribute.constructorNotPublic
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: attribute.constructorNotPublic

Rules that report this error #

  • PHPStan\Rules\Classes\ClassAttributesRule [1]
  • PHPStan\Rules\Classes\ClassConstantAttributesRule [1]
  • PHPStan\Rules\Classes\NonClassAttributeClassRule [1]
  • PHPStan\Rules\Constants\ConstantAttributesRule [1]
  • PHPStan\Rules\EnumCases\EnumCaseAttributesRule [1]
  • PHPStan\Rules\Functions\ArrowFunctionAttributesRule [1]
  • PHPStan\Rules\Functions\ClosureAttributesRule [1]
  • PHPStan\Rules\Functions\FunctionAttributesRule [1]
  • PHPStan\Rules\Functions\ParamAttributesRule [1]
  • PHPStan\Rules\Methods\MethodAttributesRule [1]
  • PHPStan\Rules\Properties\PropertyAttributesRule [1]
  • PHPStan\Rules\Properties\PropertyHookAttributesRule [1]
  • PHPStan\Rules\Traits\TraitAttributesRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.