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