Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;
}

?>

Alternatives

  • 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