- file_get_contents: documentation ( source)
- unserialize: documentation ( source)
- serialize: documentation ( source)
- error_reporting: documentation ( source)
- file_put_contents: documentation ( source)
<?php
error_reporting(E_ALL);
# export ... simulates exporting old data where class Foo didn't implement Serializable
$mode = (isset($argv[1]) && $argv[1] === 'import') ? 'import' : 'export';
if ($mode === 'export')
{
class Foo
{
protected $foo = null;
public function __construct()
{
$this->foo = "something";
}
}
$foo = new Foo();
$data = serialize($foo);
file_put_contents('/tmp/phpbug', $data);
}
else
{
class Foo implements Serializable
{
protected $foo = null;
public function __construct()
{
$this->foo = "something";
}
public function serialize()
{
echo __CLASS__ . "::serialize called\n";
return $this->foo;
}
public function unserialize($data)
{
echo __CLASS__ . "::unserialize called\n";
$this->foo = $data;
}
}
$data = file_get_contents('/tmp/phpbug');
$foo = unserialize($data);
if ($foo instanceof Foo)
echo "Thumbs up!\n";
else
echo "Unserialize FAILED\n";
}