Bug #69873 [Com]: Weird behavior with serialize(debug_backtrace()) and __sleep

From: Date: Thu, 18 Jun 2015 18:49:51 +0000
Subject: Bug #69873 [Com]: Weird behavior with serialize(debug_backtrace()) and __sleep
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-193667@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69873&edit=1 ID: 69873 Comment by: ftdysa at gmail dot com Reported by: ftdysa at gmail dot com Summary: Weird behavior with serialize(debug_backtrace()) and __sleep Status: Open Type: Bug Package: Class/Object related Operating System: Debian 6.0.10 PHP Version: 5.6.10 Block user comment: N Private report: N New Comment: 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.. Previous Comments: ------------------------------------------------------------------------ [2015-06-18 14:15:10] 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" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69873&edit=1

« previous php.bugs (#193667) next »