← Back to consistentConstructor.*
Error Identifier: consistentConstructor.private
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);
/** @consistentConstructor */
class Foo
{
private function __construct()
{
}
}
Why is it reported? #
The @consistentConstructor PHPDoc tag tells PHPStan to enforce that all child classes maintain a compatible constructor signature with the parent class. However, a private constructor cannot be overridden by child classes at all – it prevents inheritance of the constructor entirely. Marking a private constructor as consistent is contradictory because child classes cannot call or match the parent constructor.
If the class is also final, this error is not reported because final classes cannot have child classes.
How to fix it #
Change the constructor visibility to protected or public so child classes can inherit and override it:
<?php declare(strict_types = 1);
/** @consistentConstructor */
class Foo
{
- private function __construct()
+ protected function __construct()
{
}
}
Alternatively, if the class is not meant to be extended, make it final and remove the @consistentConstructor tag:
<?php declare(strict_types = 1);
-/** @consistentConstructor */
-class Foo
+final class Foo
{
private function __construct()
{
}
}
How to ignore this error #
You can use the identifier consistentConstructor.private to ignore this error using a comment:
// @phpstan-ignore consistentConstructor.private
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: consistentConstructor.private
Rules that report this error #
- PHPStan\Rules\Methods\ConsistentConstructorDeclarationRule [1]