← Back to declareStrictTypes.*
Error Identifier: declareStrictTypes.notFirst
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);
use stdClass;
declare(strict_types=1);
Why is it reported? #
PHP requires declare(strict_types=1) to be the very first statement in a file (after the opening <?php tag). If any other statement appears before it (including use imports, namespace declarations, or other code), the declare statement has no effect and PHP may emit an error. This is a fundamental language requirement, not just a convention.
How to fix it #
Move the declare(strict_types=1) statement to be the first statement in the file, immediately after the opening <?php tag:
-<?php declare(strict_types = 1);
-
-use stdClass;
-
-declare(strict_types=1);
-+<?php declare(strict_types = 1);
+
+use stdClass;
+
If the file contains inline HTML before the PHP block, the declare statement must still be the first PHP statement:
-<html>
<?php declare(strict_types = 1);
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Keywords\DeclareStrictTypesRule [1]