3v4l.org

run code in 300+ PHP versions simultaneously
<?php $classNames = array( "Foo", // userland class "StdClass", // class defined by php-src, but still not considered as internal "Bar", // userland class implementing Serializable "ReflectionClass", // internal class "ArrayObject", // internal class implementing Serializable "Baz", // userland class extending internal class implementing Serializable ); function instanitateWithoutConstructorThroughUnserialize($className) { $class = new ReflectionClass($className); try { return unserialize( sprintf( '%s:%d:"%s":0:{}', (version_compare(PHP_VERSION, '5.4', '>') && $class->implementsInterface("Serializable") ? "C" : "O"), strlen($className), $className ) ); } catch (Exception $e) { return $e; } } function instanitateWithoutConstructorThroughReflection($className) { try { $ref = new ReflectionClass($className); return $ref->newInstanceWithoutConstructor(); } catch (Exception $e) { return $e; } } class Foo {} class Bar implements Serializable { private $data; public function serialize() { return serialize($this->data); } public function unserialize($data) { $this->data = unserialize($data); } } class Baz extends ArrayObject {} foreach($classNames as $className) { echo "$className\n"; var_dump(instanitateWithoutConstructorThroughUnserialize($className)); if (version_compare(PHP_VERSION, '5.4.0') >= 0) { var_dump(instanitateWithoutConstructorThroughReflection($className)); } echo "\n"; }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Bar implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in /in/M7ENe on line 39 Foo object(Foo)#2 (0) { } object(Foo)#1 (0) { } StdClass object(stdClass)#2 (0) { } object(stdClass)#1 (0) { } Bar object(Bar)#2 (1) { ["data":"Bar":private]=> bool(false) } object(Bar)#1 (1) { ["data":"Bar":private]=> NULL } ReflectionClass object(Exception)#2 (7) { ["message":protected]=> string(51) "Unserialization of 'ReflectionClass' is not allowed" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/M7ENe" ["line":protected]=> int(15) ["trace":"Exception":private]=> array(2) { [0]=> array(4) { ["file"]=> string(9) "/in/M7ENe" ["line"]=> int(15) ["function"]=> string(11) "unserialize" ["args"]=> array(1) { [0]=> string(27) "O:15:"ReflectionClass":0:{}" } } [1]=> array(4) { ["file"]=> string(9) "/in/M7ENe" ["line"]=> int(55) ["function"]=> string(47) "instanitateWithoutConstructorThroughUnserialize" ["args"]=> array(1) { [0]=> string(15) "ReflectionClass" } } } ["previous":"Exception":private]=> NULL } object(ReflectionClass)#1 (0) { ["name"]=> uninitialized(string) } ArrayObject object(ArrayObject)#2 (1) { ["storage":"ArrayObject":private]=> array(0) { } } object(ArrayObject)#1 (1) { ["storage":"ArrayObject":private]=> array(0) { } } Baz object(Baz)#2 (1) { ["storage":"ArrayObject":private]=> array(0) { } } object(Baz)#1 (1) { ["storage":"ArrayObject":private]=> array(0) { } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
44.22 ms | 405 KiB | 8 Q