Skip to content

Instantly share code, notes, and snippets.

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 AndreasBaumgart/255524 to your computer and use it in GitHub Desktop.
Save AndreasBaumgart/255524 to your computer and use it in GitHub Desktop.
<pre><?php
class A {
private $a = 'trallala';
protected $b = array(1,2,3);
public $foo = 'bar';
}
class B extends A {
}
function typecast($v, $toType) {
$fromType = get_class($v);
$data = serialize($v);
$data = str_replace(
'O:' . strlen($fromType) . ':"' . $fromType . '":',
'O:' . strlen($toType) . ':"' . $toType . '":',
$data
);
$data = str_replace("\0$fromType\0", "\0$toType\0", $data);
return @unserialize($data);
}
$b = typecast(new A(), 'B');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment