php / php/php-src

promoted property in serialized object behaves unexpected

Open
#11,354 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Serialization Extension: standard Feature
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied PHP 8.2.6 reproducer and compare promoted readonly-property handling with the regular typed properties during unserialize(). Trace the serialization behavior involved and add or update coverage for the missing property case; done means the reproduced access yields NULL without an initialization error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.