Skip to content

[PHP] ReadOnly is now a keyword for PHP 8.1 #9629

@bshaffer

Description

@bshaffer

ReadOnly is now a keyword in PHP 8.1, meaning classes previously generated as ReadOnly now throw a PHP fatal error in PHP 8.1:

class ReadOnly {}
// PHP Parse error:  syntax error, unexpected token "readonly", expecting identifier in php shell code on line 1

To fix this, we can add readonly to the list of PHP Reserved Keywords. However, we will need to take into account the previously generated classes which had this name. To do that, we can add a class alias at the bottom:

class_alias(PBReadOnly::class, __NAMESPACE__ . '\ReadOnly');

And generate the old ReadOnly.php file as an alias, similar to what we have now for nested messages:

<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: example/v1/readonly.proto

namespace Example\V1;

class_exists(PBReadOnly::class);
@trigger_error('Example\V1\ReadOnly is deprecated and will be removed in the next major release. Use Example\V1\\PBReadOnly instead', E_USER_DEPRECATED);

This ensures that previous implementations won't break, but fixes the issue in implementations of PHP 8.1.

Thoughts on this approach? Do you think it'll work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions