Skip to content

Custom properties of DateTimeImmutable child classes are not serialized #10152

Closed
@derrabus

Description

@derrabus

Description

The following code:

<?php

class MyDateTimeImmutable extends DateTimeImmutable {
    public function __construct(
        string $datetime = "now",
        ?DateTimeZone $timezone = null,
        public ?bool $myProperty = null,
    ) {
        parent::__construct($datetime, $timezone);
    }
}

$datetime = new MyDateTimeImmutable('2022-12-22T11:26:00Z', myProperty: true);
$serialized = serialize($datetime);
$unserialized = unserialize($serialized);

var_dump($unserialized->myProperty);

Resulted in this output:

Fatal error: Uncaught Error: Typed property MyDateTimeImmutable::$myProperty must not be accessed before initialization in /path/to/file.php:17
Stack trace:
#0 {main}
  thrown in /path/to/file.php on line 17

But I expected this output instead:

bool(true)

The propblem can be observed here: https://3v4l.org/8q4Or

Apparently, MyDateTimeImmutable::$myProperty is not included in the serialized string anymore on PHP 8.2.0. On PHP 8.0/8.1, it does appear there and is unserialized properly.

PHP Version

PHP 8.2.0

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions