Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialisation of readonly properties by reference does not work (e.g. matches of preg_match) #3250

Open
snk-spo opened this issue Feb 22, 2024 · 3 comments

Comments

@snk-spo
Copy link

snk-spo commented Feb 22, 2024

Description

The following code:

https://3v4l.org/DdVMXM#v8.3.3

<?php

class A
{
    private readonly array $matches;
    
    public function __construct()
    {
        \preg_match('/t/', '', $this->matches);
    }
}

new A();

Resulted in this output:

Uncaught Error: Cannot indirectly modify readonly property A::$matches

But I expected this output instead:
The same-scope uninitialised readonly property is initialised by preg_match matches. Such by-reference initialisation of core method arguments works for local variables and untyped writable properties. I did not find anything in the documentation regarding readonly in combination with by-reference.
Even empty array (no matches) initialisation (which definitley would be a write-once operation) triggers the error.
Is this intended, a bug or an overlooked case?

If it actually is intended shouldn't it throw the following Error instead, however?

Uncaught Error: Cannot access uninitialized non-nullable property A::$matches by reference

PHP Version

PHP 8.3

Operating System

Ubuntu 23.10

@damianwadley
Copy link
Member

I believe this is intended behavior: you can't "modify" read-only properties through a reference. Only through direct assignment.

The docs aren't really clear about this, but it is alluded to as a type of non-"plain" assignment.

https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties

Modifications are not necessarily plain assignments, all of the following will also result in an Error exception:

$ref =& $test->i;
byRef($test->i);

That code was lifted straight from the RFC so it wasn't quite intended to be suitable for posting in the docs...

Copy link

github-actions bot commented Mar 8, 2024

No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you.

@github-actions github-actions bot closed this as completed Mar 8, 2024
@damianwadley
Copy link
Member

Switching this to a doc bug because I believe the code used on that page, while fine for an RFC, needs to be cleaned up some (like with added comments) to make it more suitable for general documentation.

@damianwadley damianwadley reopened this Mar 8, 2024
@damianwadley damianwadley removed the bug Documentation contains incorrect information label Mar 8, 2024
@damianwadley damianwadley transferred this issue from php/php-src Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants