Thanks to visit codestin.com
Credit goes to github.com

Skip to content

VarExporter doesn't work with Safe\DateTimeImmutable in PHP 8.2 #48754

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

Closed
miklcct opened this issue Dec 22, 2022 · 2 comments
Closed

VarExporter doesn't work with Safe\DateTimeImmutable in PHP 8.2 #48754

miklcct opened this issue Dec 22, 2022 · 2 comments

Comments

@miklcct
Copy link

miklcct commented Dec 22, 2022

Symfony version(s) affected

6.2.2

Description

The code produced by VarExporter on a Safe\DateTimeImmutable (from thecodingmachine/safe) object doesn't work with PHP 8.2. It worked on PHP 8.1.

How to reproduce

  1. Run composer require thecodingmachine/safe
  2. Run the following PHP code.
#!/usr/bin/php
<?php
declare(strict_types=1);


use Safe\DateTimeImmutable;
use Symfony\Component\VarExporter\VarExporter;


require __DIR__ . '/vendor/autoload.php'

$original = new DateTimeImmutable('2022-12-20 01:00:00');
var_export($original);
echo "\n" . $original->getTimestamp() . "\n";
$code = '$object = ' . VarExporter::export($original) . ';';
eval($code);
var_export($object);
echo "\n" . $object->getTimestamp() . "\n";

The output with PHP 8.1:

Safe\DateTimeImmutable::__set_state(array(
   'innerDateTime' => 
  DateTimeImmutable::__set_state(array(
     'date' => '2022-12-20 01:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'Europe/London',
  )),
   'date' => '2022-12-20 01:00:00.000000',
   'timezone_type' => 3,
   'timezone' => 'Europe/London',
))
1671498000
Safe\DateTimeImmutable::__set_state(array(
   'innerDateTime' => 
  DateTimeImmutable::__set_state(array(
     'date' => '2022-12-20 01:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'Europe/London',
  )),
   'date' => '2022-12-20 01:00:00.000000',
   'timezone_type' => 3,
   'timezone' => 'Europe/London',
))
1671498000

The output with PHP 8.2:

\Safe\DateTimeImmutable::__set_state(array(
   'innerDateTime' => 
  \DateTimeImmutable::__set_state(array(
     'date' => '2022-12-20 01:00:00.000000',
     'timezone_type' => 3,
     'timezone' => 'Europe/London',
  )),
   'date' => '2022-12-20 01:00:00.000000',
   'timezone_type' => 3,
   'timezone' => 'Europe/London',
))
1671498000
\Safe\DateTimeImmutable::__set_state(array(
   'innerDateTime' => NULL,
   'date' => '2022-12-20 01:00:00.000000',
   'timezone_type' => 3,
   'timezone' => 'Europe/London',
))Error: Call to a member function getTimestamp() on null in file /home/michael/projects/national_rail_timetable/vendor/thecodingmachine/safe/lib/DateTimeImmutable.php on line 258
Stack trace:
  1. Error->() /home/michael/projects/national_rail_timetable/vendor/thecodingmachine/safe/lib/DateTimeImmutable.php:258
  2. Safe\DateTimeImmutable->getTimestamp() /home/michael/projects/national_rail_timetable/bin/test.php:16

Possible Solution

No response

Additional Context

No response

@miklcct miklcct added the Bug label Dec 22, 2022
miklcct added a commit to miklcct/rail_open_timetable_data that referenced this issue Dec 22, 2022
It fails with caching in PHP 8.2. Bug reported at symfony/symfony#48754
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Dec 22, 2022

Thanks for the reproducer.
If you use serialize+unserialize instead of VarExporter, you'll observe the exact same behavior.
This is a PHP bug. Please report it to php-src.
While doing so, please provide them with a standalone reproducer if possible.
The issue I think is that DateTimeImmutable::__serialize/__unserialize doesn't take properties added by child classes into account.

@derrabus
Copy link
Member

Reported as php/php-src#10152

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