-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Cannot hydrate a DateTime instance created without the constructor #8152
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
Comments
Why not use Though I do think that we also need to migrate away from |
I could work with that I think, but then I figured this doesn't work with child classes of eg class Foo extends DateTime
{
}
echo get_class(Foo::__set_state([
'date' => '1970-01-01 00:00:00.000000',
'timezone_type' => 1,
'timezone' => '+00:00',
])); Displays |
you can set dummy error handler and remove it after the object is hydrated |
Well, that's not a proper solution. It will break on PHP 9 if we don't do anything else. Something that works for class Foo extends DateTime
{
}
$foo = new Foo();
(new ReflectionMethod('DateTime', '__construct'))->invoke($foo, ...) But for Another solution would be to implement __serialize/__unserialize in PHP 8.2. We'd still have to try to ensure that pre8.2 payloads are still serializable. |
Why wouldn't it make sense to declare these properties (without initializing them)? So that writing to them is not a dynamic property access. I see no concrete disadvantages to that. |
That would be my preference. Pre-8.2 payloads should still unserialize fine, because the __wakeup and __unserialize formats are compatible. |
Some historical perspective:
None of the I do find it odd that |
FWIW, I've started adding the __serialize/__unserialize methods for the Date classes. |
PR 1: #8422 |
The DateTime/DateTimeImmutable/DateTimeZone part is now merged. |
PR 2: #8459 (for DateInterval) |
PR 3: #8464 (for DatePeriod) |
All the classes are now covered, and their PRs merged, so closing this ticket. |
Description
This report applies to DateTime, DateTimeImmutable, DateTimeZone, DateInterval and DatePeriod.
Before PHP 8.2, there used to be a way to create a DateTime object from an uninitialized prototype:
These 3 properties do not exist on
DateTime
, but they're still returned byserialize($date)
, that's how I figured out how to hydrate the previous object. And it works perfectly. But since PHP 8.2, a deprecation is triggered because dynamic properties are not allowed.This means that since 8.2, there is no way to hydrate a
DateTime
instance that is created without the constructor.This is an issue in symfony/var-exporter for example, which is a library that provides a way to serialize objects to native PHP code (instead of regular serialization strings.) I cannot figure out a way to make this lib support PHP 8.2 while not triggering a warning.
Note that the engine allows itself to create those dynamic properties even on PHP 8.2. Here is some code to highlight this:
This outputs:
A simple fix would be to add
#[AllowDynamicProperties]
to those classes.Another fix would be to actually declare the corresponding properties as protected.
WDYT?
The text was updated successfully, but these errors were encountered: