Bug #69873 [NEW]: Weird behavior with serialize(debug_backtrace()) and __sleep
| From: | ftdysa at gmail dot com | Date: | Thu, 18 Jun 2015 14:15:10 +0000 |
| Subject: | Bug #69873 [NEW]: Weird behavior with serialize(debug_backtrace()) and __sleep | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-193659@lists.php.net to get a copy of this message | ||
From: ftdysa at gmail dot com
Operating system: Debian 6.0.10
PHP version: 5.6.10
Package: Class/Object related
Bug Type: Bug
Bug description:Weird behavior with serialize(debug_backtrace()) and __sleep
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 bug report at https://bugs.php.net/bug.php?id=69873&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69873&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69873&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69873&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69873&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69873&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69873&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69873&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69873&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69873&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69873&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69873&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69873&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69873&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69873&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69873&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69873&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69873&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69873&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69873&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69873&r=mysqlcfg