php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69873 Weird behavior with serialize(debug_backtrace()) and __sleep
Submitted: 2015-06-18 14:15 UTC Modified: 2015-06-18 20:11 UTC
From: ftdysa at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.6.10 OS: Debian 6.0.10
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ftdysa at gmail dot com
New email:
PHP Version: OS:

 

 [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"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-18 18:49 UTC] 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 20:11 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-06-18 20:11 UTC] cmb@php.net
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>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 23:01:28 2024 UTC