php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29754 Serialization does not work when I use private/protected members and __sleep()
Submitted: 2004-08-19 13:15 UTC Modified: 2004-08-24 17:21 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: mytrash at seznam dot cz Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.1 OS: Windows XP
Private report: No CVE-ID: None
 [2004-08-19 13:15 UTC] mytrash at seznam dot cz
Description:
------------
Hello.

I use PHP session. When I use private or protected members in my class and don't use magic method __sleep(), PHP serialization will serialize all members and their values into PHP session correctly.
But when I use the same class and implement magic method __sleep(), PHP serialization will serialize correctly ONLY public members. Private and protected members (that I want serialize - I specified them in array returned from __sleep() function) have NULL value.
In my example's result is string from session file generated by PHP.

T.Zkoumalek

Reproduce code:
---------------
class Test
{
    private   $a = "Variable a";
    protected $b = "Variable b";
    public    $c = "Variable c";
    public    $d = "This need not be saved.";


    // Save only $a, $b, $c.
    function __sleep()
    {
        return Array( "a", "b", "c" );
    }
}

session_start();
$_SESSION[ "test" ] = new Test();

Expected result:
----------------
test|O:4:"Test":3:{s:7:" Test a";s:10:"Variable a";s:4:" * b";s:10:"Variable b";s:1:"c";s:10:"Variable c";}

Actual result:
--------------
test|O:4:"Test":3:{s:1:"a";N;s:1:"b";N;s:1:"c";s:10:"Variable c";}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-24 17:21 UTC] sfox@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

This report is a duplicate of #26737, which was closed on 21st August following a patch by Andrey and Curt..
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 18 21:01:26 2024 UTC