Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom properties of DateTimeImmutable child classes are not serialized #10152

Closed
derrabus opened this issue Dec 22, 2022 · 6 comments
Closed

Comments

@derrabus
Copy link
Contributor

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

@cmb69
Copy link
Member

cmb69 commented Dec 22, 2022

This is because DateTime(Immutable) (and DateTimeZone) now implement ::__(un)serialize() (#8422). Besides this is not documented, it looks like subclasses have been overlooked. Or is that deliberate, @derickr?

@derickr
Copy link
Member

derickr commented Dec 23, 2022

This is not deliberate, and hence a bug.

derickr added a commit to derickr/php-src that referenced this issue Jan 25, 2023
derickr added a commit to derickr/php-src that referenced this issue Jan 31, 2023
@OskarStark
Copy link

Thank you very much @derickr 🎉 ☀️

@OskarStark
Copy link

Was this released in 8.2.3, because I cannot find anything in the changelog

cc @mvhirsch

@TimWolla
Copy link
Member

8.2.3 was an irregular release that was coordinated with other projects. It only contains the security fixes. The fix is included in the changelog for 8.2.4:

php-src/NEWS

Lines 22 to 23 in 8c87a5c

. Fix GH-10152 (Custom properties of Date's child classes are not
serialised). (Derick)

@OskarStark
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants