Menu
Error Identifier: new.interface
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);
interface LoggerInterface
{
public function log(string $message): void;
}
$logger = new LoggerInterface();
Why is it reported? #
PHP interfaces cannot be instantiated. An interface defines a contract that classes must implement, but it does not provide concrete implementations of its methods. Attempting to use new with an interface name will result in a fatal error at runtime.
How to fix it #
Instantiate a concrete class that implements the interface instead.
<?php declare(strict_types = 1);
-$logger = new LoggerInterface();
+$logger = new FileLogger();
How to ignore this error #
You can use the identifier new.interface to ignore this error using a comment:
// @phpstan-ignore new.interface
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.interface
Rules that report this error #
- PHPStan\Rules\Classes\InstantiationRule [1]