strict_types declaration must not

Description

declare() supports a block form for most directives, such as declare(ticks = 1) { ... }, which scopes the setting to the code inside the block. strict_types is different: it is a compile-time, whole-file setting, not something that can be toggled for a portion of the script. Because of that, the block form is rejected outright.

The full message reads: strict_types declaration must not use block mode.

Example

<?php

declare(strict_types = 1) {
    var_dump(strlen('abc'));
}

?>

Literal Examples

  • strict_types declaration must not use block mode

Solutions

  • Remove the curly braces, and use declare(strict_types = 1); as a simple statement.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.