Open
Description
Description
The following code:
<?php
class Foo {
public string $bar = 'a';
public ?string $baz = null;
public function __construct(public readonly ?string $x = null) {
}
}
$fooSerialized = 'O:3:"Foo":1:{s:3:"bar";s:1:"a";}a';
$fooUnserialized = unserialize($fooSerialized);
var_dump($fooUnserialized->bar); // works because exists in serialized string
var_dump($fooUnserialized->baz); // works because regular typed property, even if not existing in serialized string
var_dump($fooUnserialized->x); // Doesn't work
Resulted in this output:
string(1) "a"
NULL
Fatal error: Uncaught Error: Typed property Foo::$x must not be accessed before initialization in /in/4d6XG:18
Stack trace:
#0 {main}
thrown in /in/4d6XG on line 18
Process exited with code 255.
But I expected this output instead:
string(1) "a"
NULL
NULL
See also https://3v4l.org/4d6XG#v8.2.6 (all versions)
PHP Version
PHP 8.2.6
Operating System
No response