Skip to content

Instantly share code, notes, and snippets.

@slusarz
Created February 4, 2013 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slusarz/4709613 to your computer and use it in GitHub Desktop.
Save slusarz/4709613 to your computer and use it in GitHub Desktop.
Test script: serialize incorrectly saving objects when they are cloned
<?php
class A
{
public $a = array();
public function __construct()
{
$this->a[] = new B(1);
$this->a[] = new B(2);
}
}
class B implements Serializable
{
public $b;
public function __construct($c)
{
$this->b = new C($c);
}
public function serialize()
{
return serialize(clone $this->b);
}
public function unserialize($data)
{
$this->b = unserialize($data);
}
}
class C
{
public $c;
public function __construct($c)
{
$this->c = $c;
}
}
$a = unserialize(serialize(new A()));
print $a->a[0]->b->c . "\n";
print $a->a[1]->b->c . "\n";
// Expected:
// 1
// 2
// Actual:
// 1
// PHP Notice: Trying to get property of non-object in /tmp/test.php on line 47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment