|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-02-17 06:27 UTC] mastabog at hotmail dot com
Description:
------------
First of all, I know this is very new and undocumented.
The Serializable interface serialize() method breaks reference of objects that are properties of the serialized object and that they themselves implement the Serializable interface. See the reproduceable code below.
an echo over $ser yields:
C:1:"C":85:{a:2:{s:1:"A";C:1:"A":6:{a:0:{}}s:1:"B";C:1:"B":32:{a:1:{s:1:"A";C:1:"A":6:{a:0:{}}}}}}
It's visible that the last A is not a reference but a new class instance.
I know that Serializable::unserialize() acts as a constructor, but shouldn't object references be honored by Serializable::serialize() the same way unserialize() does when the class does not implement the Serializable interface.
If we remove the Serializable interface from class A and leave it like this:
class A {}
then $ser looks like the following:
O:1:"C":2:{s:1:"A";O:1:"A":0:{}s:1:"B";O:1:"B":1:{s:1:"A";r:2;}}
And it's visible that the last A is a reference.
If this is all intended behavior for the Serializable interface to break object references then you can ignore this bug report. I hope it's not though, because it would have provided a better alternative to the __sleep() and __wakeup() (e.g. classes extending the PDO class cannot be serialized using __sleep() and __wakeup(), neither by overloading nor by default)
Reproduce code:
---------------
class A implements Serializable
{
public function serialize ()
{
$serialized = array();
foreach($this as $prop => $val) {
$serialized[$prop] = $val;
}
return serialize($serialized);
//return serialize(get_object_vars($this));
}
function unserialize($serialized)
{
foreach(unserialize($serialized) as $prop => $val) {
$this->$prop = $val;
}
return true;
}
}
class B extends A
{
public $A;
}
class C extends A
{
public $A;
public $B;
}
$oC = new C();
$oC->A = new A();
$oC->B = new B();
$oC->B->A = $oC->A;
echo $oC->A === $oC->B->A ? "yes" : "no", "\n";
$ser = serialize($oC);
$new_oC = unserialize($ser);
echo $new_oC->A === $new_oC->B->A ? "yes" : "no", "\n";
Expected result:
----------------
yes
yes
Actual result:
--------------
yes
no
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
When playing around with this bug, I discovered this: While var_dump($new_oC->A === $new_oC->B->A); Results into bool(false) $this->assertEquals($new_oC->A, $new_oC->B->A); Does not fail!To make this fail, it needs to be $this->assertEquals(true, $new_oC->A === $new_oC->B->A);Crashing also reproducable. Win2000Prof / PHP514 My related problem is that serializing a Serializable object, thats some object property holds a reference to the serialized root object, PHP crashes. The magic function __serialize have too much sideFx for me - making the object unworkable, but I cant figure out how nested serialize function calls could work properly. Probably when the native serialize function is handles a nested call then could hook up the root calls pointer map? And then could this work for nested unserialization? At this time I have no vision about the sideFx of this mention. Right now I dont have a prepared C development environment to work out, and dont remember how the related part of the code workx. As I remember nested call test was implemented in the __autoload() function. Since it can cause crashes please take more care on this bug. Just uncomment 'implements Serializable' to let it crash. class A // implements Serializable { public $dontSerialize; public $that; function __construct($that = null) { $this->that = ($that === null) ? new A($this) : $that; } function serialize() { return serialize($this->that); } function unserialize($serialized) { $this->that = serialize($serialized); } }Yes its expected, but IMHO the problem need a featureful fix, not a limitation. Im not sure my problem is SPL related. Here is my workaround: The native serialize must start counting variables from 1. Outer references may indexed negative, in case of nested call. Nested native calls can only called in a native calls context in case of handling a __serialize() or Serializable::serialize() call. The nested call can reference as -1 to the topmost calls serialized root object. In case of a negative reference is unresolvable on unserialization (multiplying by -1 and querying the topmost pointer remapping), the value is not ready, and a null value replaces temporarily. What about a nested call already serialized a variable that the topmost is willing to serialize? we have that pointer in the map, but the reference no. might be greater than the number of locally serialized variables. If every call starts numbering variables from 1, then the topmost serialize call also could reference on those hidden variables by a negative index from the nesting wide accessible map - not the calls local map. Backwards if the unserialization process makes it possible, the negative reference is also resolvable at the time the function processes it. In case of wrong unserialization order, the negative reference's zval is created with null, and the time the unserialization reaches that index, da value's pointer is ready to be casted properly. I think above mentioned way doesnt violates the syntax or the result of serialization, this could work for a lot of cases, just like mine. This way, the following code could produce this: C:1:"A":93:{a:2:{s:4:"that";C:1:"A":43:{a:2:{s:4:"that";R:-1;s:9:"thatsThat";R:-2;}}s:9:"thatsThat";R:-1;} class A implements Serializable { public $dontSerialize; public $that; public $thatsThat; function __construct($that = null) { $this->that = ($that === null) ? new A($this) : $that; $this->thatsThat = &$this->that->that; } function serialize() { return serialize(array($this->that, $this->thatsThat)); } function unserialize($serialized) { list($this->that, $this->thatsThat) = unserialize($serialized); } } echo serialize(new A()); Some more without any class relation, the following script serialises 3 arrays instead of 2 since we are passing a copy of $arr1, but the _deprecated_ syntax serialize(&$arr1) still works fine. Documentation says: "recent versions of PHP you will get a warning saying that "Call-time pass-by-reference" is deprecated when you use a & in foo(&$a);" References' syntax are not consequent. $arr2 = array(); $arr1['that'] = &$arr2; $arr2['that'] = &$arr1; $arr1['thatsThat'] = &$arr2['that']; $arr2['thatsThat'] = &$arr1['that']; echo serialize($arr1); Produces: a:2:{s:4:"that";a:2:{s:4:"that";a:2:{s:4:"that";R:2;s:9:"thatsThat";R:3;}s:9:"thatsThat";R:2;}s:9:"thatsThat";R:3;} instead of a:2:{s:4:"that";a:2:{s:4:"that";R:1;s:9:"thatsThat";R:2;}s:9:"thatsThat";R:1;}The reproduce code is wrong. Your code serializes everything on it's own, as you can see if you replace return $serialized; with $serialized = serialize($serialized); printf("serialized %s as '%s'\n", get_class($this), $serialized); return $serialized; Output: yes serialized A as 'a:0:{}' serialized A as 'a:0:{}' serialized B as 'a:1:{s:1:"A";C:1:"A":6:{a:0:{}}}' serialized C as 'a:2:{s:1:"A";C:1:"A":6:{a:0:{}}s:1:"B";C:1:"B":32:{a:1:{s:1:"A";C:1:"A":6:{a:0:{}}}}}' noI still don't understand why this is not seen as a bug. My example shows that without "implements Serializable" object references are honoured (as expected) while with "implements Serializable" object references are broken (which is unexpected). Seeing you classified this as "feature request" makes me think that breaking object references was actually intended behaviour or that there is a way to maintain object references when implementing Serializable. Can you then please provide the body of the serialize() and unserialize() methods in class A in the example below that will maintain object references as expected, i.e. $new_oC->A === $new_oC->B->A? class A implements Serializable { public function serialize () { [...] } function unserialize($serialized) { [...] } } class B extends A { public $A; } class C extends A { public $A; public $B; } $oC = new C(); $oC->A = new A(); $oC->B = new B(); $oC->B->A = $oC->A; echo $oC->A === $oC->B->A ? "yes" : "no", "\n"; $ser = serialize($oC); $new_oC = unserialize($ser); echo $new_oC->A === $new_oC->B->A ? "yes" : "no", "\n";