php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42239 class member is broken after retrieving from a session
Submitted: 2007-08-08 00:02 UTC Modified: 2007-08-08 13:25 UTC
From: peter dot evertz at snafu dot de Assigned:
Status: Not a bug Package: Session related
PHP Version: 5.2.4RC1 OS: Linux 2.6.18
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: peter dot evertz at snafu dot de
New email:
PHP Version: OS:

 

 [2007-08-08 00:02 UTC] peter dot evertz at snafu dot de
Description:
------------
I found this while using propel. It uses DateTime class for storing.
After retrieving the class from a Session the member is broken.

The code creates a session variabel "v" with the given class if not found.On the second call to the script the class is retrievies from session. The member x is broken.


Reproduce code:
---------------
<?php
  class c {
    protected $v;

    function print_type() {
      if ( $this->v instanceof DateTime ) {
        print "x is DateTime\n";
      }
    }
    function get_v() {
      return $this->v;
    }
    function set_v($i) {
      $this->v=$i;
    }
  }

session_start();

if ( !isset($_SESSION["x"] )) {
  $x = new c();
  $x->set_v( new DateTime());
  $_SESSION["x"]=$x;
  print "New object created\n";
}

$_SESSION["x"]->print_type();
print $_SESSION["x"]->get_v()->format("d.m.Y");
?>


Expected result:
----------------
First call:
New object created x is DateTime 08.08.2007

Second call:
x is DateTime 08.08.2007

Actual result:
--------------
First call:
New object created x is DateTime 08.08.2007

Second call:
x is DateTime Warning: DateTime::format(): The DateTime object has not been correctly initialized by its constructor in /home/peter/workspace/Kurse/html/t1.php on line 28

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-08 12:14 UTC] johannes@php.net
Objects of internal classes can't be serialized and therefore can't be stored in a session. You might use __sleep/__wakeup magic methods to store the timestamp in the session and then restore it.
 [2007-08-08 13:25 UTC] peter dot evertz at snafu dot de
Sorry for writing a Bug-report. And thank you for your hint for a work-around.

 
Are there plans to remove this limitation ?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC