Bug #69873 [Opn->Csd]: Weird behavior with serialize(debug_backtrace()) and __sleep
| From: | cmb@php.net | Date: | Thu, 18 Jun 2015 20:11:53 +0000 |
| Subject: | Bug #69873 [Opn->Csd]: Weird behavior with serialize(debug_backtrace()) and __sleep | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-193671@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
Updated by: cmb@php.net
Reported by: ftdysa at gmail dot com
Summary: Weird behavior with serialize(debug_backtrace()) and
__sleep
-Status: Open
+Status: Closed
Type: Bug
Package: Class/Object related
Operating System: Debian 6.0.10
PHP Version: 5.6.10
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
I suggest to turn on error_reporting during development; then
you'd get a respective notice, see <http://3v4l.org/hN9eJ>.
Previous Comments:
------------------------------------------------------------------------
[2015-06-18 18:49:51] ftdysa at gmail dot com
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..
------------------------------------------------------------------------
[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