Menu

← Back to new.*

Error Identifier: new.protectedConstructor

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

class Base
{
	protected function __construct()
	{
	}

	public static function create(): static
	{
		return new static();
	}
}

new Base();

Why is it reported? #

A class with a protected constructor is being instantiated from outside the class hierarchy. A protected constructor restricts instantiation to within the class itself and its subclasses. Attempting to call new on such a class from outside the class hierarchy will cause a fatal error at runtime.

How to fix it #

Use the provided factory method instead of calling new directly:

-new Base();
+Base::create();

Or if the constructor should be accessible from outside the class, change its visibility:

 class Base
 {
-	protected function __construct()
+	public function __construct()
 	{
 	}
 }

Or instantiate the class from within a subclass:

 class Child extends Base
 {
 	public static function createChild(): self
 	{
 		return new self();
 	}
 }

How to ignore this error #

You can use the identifier new.protectedConstructor to ignore this error using a comment:

// @phpstan-ignore new.protectedConstructor
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: new.protectedConstructor

Rules that report this error #

  • PHPStan\Rules\Classes\InstantiationRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.