3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function ClassTypeCast(&$obj,$class_type){ if(class_exists($class_type,true)) { $obj = unserialize(preg_replace("/^O:[0-9]+:\"[^\"]+\":/i", "O:".strlen($class_type).":\"".$class_type."\":", serialize($obj))); } } class MyClass { private $foo = null; function setString($str) { $this->foo = $str; } function awful() { return "This is awful!"; } } class MyClass2 { private $foo2 = null; function setString2($str) { $this->foo2 = $str; } function awful() { return "This is awesome!" . PHP_EOL; } } $my1 = new MyClass(); $my1->setString("kill me now"); ClassTypeCast($my1, "MyClass2"); echo $my1->awful() . PHP_EOL; var_dump($my1);
Output for 5.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
This is awesome! object(MyClass2)#2 (2) { ["foo2":"MyClass2":private]=> NULL ["foo":"MyClass":private]=> string(11) "kill me now" }
Output for 5.0.2 - 5.2.17
This is awesome! object(MyClass2)#2 (2) { ["foo2:private"]=> NULL ["foo:private"]=> string(11) "kill me now" }
Output for 5.0.0 - 5.0.1
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/TMDfr on line 29 Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/TMDfr on line 37 This is awesome!PHP_EOLPHP_EOLobject(MyClass2)#2 (2) { ["foo2:private"]=> NULL ["foo:private"]=> string(11) "kill me now" }
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/TMDfr on line 12
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/TMDfr on line 12
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/TMDfr on line 12
Process exited with code 255.