syntax error, unexpected token “#[”, expecting “)”

Description

Attributes can only be placed in front of the declarations that support them, such as classes, functions, methods and parameters. A catch block’s caught-exception clause does not accept an attribute: PHP was expecting the closing parenthesis of the catch (...) clause, and found the beginning of an attribute, #[, instead.

Example

<?php

try {
} catch (Exception #[SomeAttribute] $e) {
}

?>

Solutions

  • Remove the attribute from the catch clause.

  • Move the attribute to a declaration that supports it, such as the enclosing method.

See Also

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>`_.