syntax error, unexpected identifier “readonly”, expecting “function” or “const”

Description

This syntax error signals that readonly is still considered as an identifier, and not as a keyword. This happens when the underlying PHP version is too old for the source code.

Example

<?php

class X {
    readonly int $property;
}

?>

Solutions

  • Remove the readonly option.

  • Upgrade to PHP 8.1 or more recent.

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

Static Analysis

This error may be tracked down with the following static analysis rules: Classes/ReadonlyUsage.