Skip to content

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

@ghost

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions