- preg_match: documentation ( source)
- unserialize: documentation ( source)
- serialize: documentation ( source)
- sprintf: documentation ( source)
<?php
class Foo
{
function test()
{
echo 'foo test';
}
}
class Bar
{
function test()
{
echo 'bar test';
}
}
$foo = new Foo();
$foo = change_class($foo, 'Bar');
$foo->test();
function change_class($object, $new_class) {
preg_match('~^O:[0-9]+:"[^"]+":(.+)$~', serialize($object), $matches);
return unserialize(sprintf('O:%s:"%s":%s', strlen($new_class), $new_class, $matches[1]));
}