|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-06-18 14:15 UTC] ftdysa at gmail dot com
Description: ------------ I am seeing weird behavior that I believe was introduced by this patch: https://bugs.php.net/bug.php?id=69210 It seems that if you call serialize(debug_backtrace()) where the backtrace contains an object with a __sleep method that returns a non-string (in this case, an array containing a member variable of that object), the member variable is overridden with the __toString representation of that object. That's a bit confusing to understand but I hope my example code will illustrate it better. Test script: --------------- <?php class Obj { public function __construct($mix) { foreach ($mix as $k => $v) { $this->$k = $v; } } public function __toString() { $str = ''; foreach ($this as $k => $v) { $str = "$k => $v\n"; } return $str; } } class A { public $obj; public function setObj($obj) { $this->obj = $obj; } public function getObj() { return $this->obj; } function __sleep() { return ['test' => $this->obj]; } } function printA(A $a) { var_dump($a); serialize(debug_backtrace(0)); } $obj = new Obj(['hello'=>'there']); $a = new A(); $a->setObj($obj); var_dump($a->getObj()); printA($a); var_dump($a->getObj()); Expected result: ---------------- object(Obj)#1 (1) { ["hello"]=> string(5) "there" } object(A)#2 (1) { ["obj"]=> object(Obj)#1 (1) { ["hello"]=> string(5) "there" } } object(Obj)#1 (1) { ["hello"]=> string(5) "there" } Actual result: -------------- object(Obj)#1 (1) { ["hello"]=> string(5) "there" } object(A)#2 (1) { ["obj"]=> object(Obj)#1 (1) { ["hello"]=> string(5) "there" } } string(15) "hello => there" PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 18 02:00:02 2025 UTC |
On further review, this is probably expected behavior. changing sleep to: public function __sleep() { return ['obj']; } Returns the expected result. I must say that this is a rather weird side effect..