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 ); foreach($classNames as $className) { print_r("-----------$className-----------\n"); var_dump(instanitateWithoutConstructorThroughUnserialize($className)); if (version_compare(PHP_VERSION, '5.4.0') >= 0) { var_dump(instanitateWithoutConstructorThroughReflection($className)); } print_r("----------------------\n"); } function instanitateWithoutConstructorThroughUnserialize($className) { return unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className)); } function instanitateWithoutConstructorThroughReflection($className) { try { $ref = new ReflectionClass($className); return $ref->newInstanceWithoutConstructor(); } catch (ReflectionException $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); } }
Output for git.master, git.master_jit, rfc.property-hooks
-----------Foo----------- object(Foo)#1 (0) { } object(Foo)#2 (0) { } ---------------------- -----------StdClass----------- object(stdClass)#2 (0) { } object(stdClass)#1 (0) { } ---------------------- -----------Bar----------- object(__PHP_Incomplete_Class)#1 (1) { ["__PHP_Incomplete_Class_Name"]=> string(3) "Bar" } object(ReflectionException)#2 (7) { ["message":protected]=> string(26) "Class "Bar" does not exist" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(-1) ["file":protected]=> string(9) "/in/hvY2C" ["line":protected]=> int(26) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(9) "/in/hvY2C" ["line"]=> int(26) ["function"]=> string(11) "__construct" ["class"]=> string(15) "ReflectionClass" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(3) "Bar" } } [1]=> array(4) { ["file"]=> string(9) "/in/hvY2C" ["line"]=> int(15) ["function"]=> string(46) "instanitateWithoutConstructorThroughReflection" ["args"]=> array(1) { [0]=> string(3) "Bar" } } } ["previous":"Exception":private]=> NULL } ---------------------- -----------ReflectionClass----------- Fatal error: Uncaught Exception: Unserialization of 'ReflectionClass' is not allowed in /in/hvY2C:21 Stack trace: #0 /in/hvY2C(21): unserialize('O:15:"Reflectio...') #1 /in/hvY2C(13): instanitateWithoutConstructorThroughUnserialize('ReflectionClass') #2 {main} thrown in /in/hvY2C on line 21
Process exited with code 255.

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:
41.95 ms | 403 KiB | 8 Q