php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65151 DateTime subclasses cant use custom serializer
Submitted: 2013-06-27 21:18 UTC Modified: 2020-04-20 15:21 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:0 of 0 (0.0%)
From: mike at silverorange dot com Assigned: cmb (profile)
Status: Not a bug Package: Date/time related
PHP Version: 5.3.26 OS: CentOS or RHEL
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mike at silverorange dot com
New email:
PHP Version: OS:

 

 [2013-06-27 21:18 UTC] mike at silverorange dot com
Description:
------------
If a DateTime subclass implements Serializable it cant be unserialized. A PHP 
warning is raised when restoring the time zone is attempted.

Test script:
---------------
class MyDate extends DateTime implements Serializable
{
    public function serialize()
    {
        $data = array(
            $this->getTimestamp(),
            $this->dateTimezone()->getName()
        );

        return serialize($data);
    }

    public function unserialize($data)
    {
        $data = unserialize($data);
        $this->setTimestamp($data[0]);
        $this->setTimezone(new DateTimeZone($data[1]));
    }
}

$d = new MyDate('2013-01-01', new DateTimeZone('UTC'));
echo unserialize(serialize($d))->format('c');

Expected result:
----------------
2013-01-01T00:00:00+00:00

Actual result:
--------------
Warning: DateTime::setTimezone(): The DateTime object has not been correctly 
initialized by its constructor

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-18 05:23 UTC] seth dot johnson+phpnet at gmail dot com
This is not a bug.

An unserialize implementation is responsible for properly constructing the entire object and that includes calling its parent's constructor if that would normally be necessary.  You would have the same problem if you overrode DateTime's constructor and did not call parent::__construct.

It's fairly easy to address in code though.  Just call the parent's constructor in unserialize, e.g.

    public function unserialize($data)
    {
        parent::__construct(null);
        $data = unserialize($data);
        $this->setTimestamp($data[0]);
        $this->setTimezone(new DateTimeZone($data[1]));
    }
 [2020-04-20 15:21 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-04-20 15:21 UTC] cmb@php.net
Indeed, it is like Seth has explained.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 11 03:01:29 2024 UTC