Error Identifier: new.abstract
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);
abstract class Foo
{
abstract public function doSomething(): void;
}
$foo = new Foo(); // error: Instantiated class Foo is abstract.
Why is it reported? #
Abstract classes cannot be instantiated directly in PHP. They are meant to be extended by concrete (non-abstract) subclasses that implement all abstract methods. Attempting to use new on an abstract class will result in a fatal error at runtime.
How to fix it #
Create a concrete subclass that implements all abstract methods and instantiate that instead.
abstract class Foo
{
abstract public function doSomething(): void;
}
+class ConcreteFoo extends Foo
+{
+ public function doSomething(): void
+ {
+ // implementation
+ }
+}
+
-$foo = new Foo();
+$foo = new ConcreteFoo();
How to ignore this error #
You can use the identifier new.abstract to ignore this error using a comment:
// @phpstan-ignore new.abstract
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.abstract
Rules that report this error #
- PHPStan\Rules\Classes\InstantiationRule [1]