Menu

← Back to new.*

Error Identifier: new.static

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 Foo
{
	public function create(): static
	{
		return new static();
	}
}

Why is it reported? #

Using new static() in a non-final class is unsafe because child classes may override the constructor with different parameters or requirements. When new static() is called, it creates an instance of the actual runtime class (which may be a subclass), but uses the parent’s constructor call. If a child class changes the constructor signature, this will break.

Learn more: Solving PHPStan error “Unsafe usage of new static()”

How to fix it #

Mark the class as final if it is not meant to be extended:

-class Foo
+final class Foo
 {
 	public function create(): static
 	{
 		return new static();
 	}
 }

Or mark the constructor as final to prevent subclasses from changing its signature:

 class Foo
 {
+	final public function __construct()
+	{
+	}
+
 	public function create(): static
 	{
 		return new static();
 	}
 }

Or add @phpstan-consistent-constructor to the class to declare that all subclasses must have a compatible constructor:

+/** @phpstan-consistent-constructor */
 class Foo
 {
 	public function create(): static
 	{
 		return new static();
 	}
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Classes\NewStaticRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.