syntax error, unexpected token “private”, expecting “=”

Description

The name of class constant could not use PHP keywords, such as the visibilities private, protected and public; optiosn such as abstract or final, until PHP 8.3. Until then, they would generate a parse error. Such names are generally not used, as they easily sow confusion in the code, at definition but also at usage.

Example

<?php

class X {
    private const string private = 'protected';
}

?>

Solutions

  • Use another name for the constant.

  • Upgrade to PHP 8.3 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>`_.