@ 2014-06-17T12:35:17Z <?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);
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 -----------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 . Output for 8.0.0 - 8.0.30 -----------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-----------
Warning: Erroneous data format for unserializing 'ReflectionClass' in /in/hvY2C on line 21
Notice: unserialize(): Error at offset 26 of 27 bytes in /in/hvY2C on line 21
bool(false)
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
Fatal error: Uncaught UnexpectedValueException: Incomplete or ill-typed serialization data in /in/hvY2C:21
Stack trace:
#0 [internal function]: ArrayObject->__unserialize(Array)
#1 /in/hvY2C(21): unserialize('O:11:"ArrayObje...')
#2 /in/hvY2C(13): instanitateWithoutConstructorThroughUnserialize('ArrayObject')
#3 {main}
thrown in /in/hvY2C on line 21
Process exited with code 255 . Output for 7.4.0 - 7.4.33 -----------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(24) "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-----------
Warning: Erroneous data format for unserializing 'ReflectionClass' in /in/hvY2C on line 21
Notice: unserialize(): Error at offset 26 of 27 bytes in /in/hvY2C on line 21
bool(false)
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
Fatal error: Uncaught UnexpectedValueException: Incomplete or ill-typed serialization data in /in/hvY2C:21
Stack trace:
#0 [internal function]: ArrayObject->__unserialize(Array)
#1 /in/hvY2C(21): unserialize('O:11:"ArrayObje...')
#2 /in/hvY2C(13): instanitateWithoutConstructorThroughUnserialize('ArrayObject')
#3 {main}
thrown in /in/hvY2C on line 21
Process exited with code 255 . Output for 7.3.32 - 7.3.33 -----------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(24) "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-----------
object(ReflectionClass)#2 (1) {
["name"]=>
string(0) ""
}
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
Warning: Erroneous data format for unserializing 'ArrayObject' in /in/hvY2C on line 21
bool(false)
object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
----------------------
Output for 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.17 - 7.2.33 , 7.3.0 - 7.3.31 -----------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(24) "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-----------
object(ReflectionClass)#2 (1) {
["name"]=>
string(0) ""
}
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
Warning: Erroneous data format for unserializing 'ArrayObject' in /in/hvY2C on line 21
Notice: unserialize(): Error at offset 22 of 23 bytes in /in/hvY2C on line 21
bool(false)
object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
----------------------
Output for 5.4.0 - 5.4.28 , 5.4.30 - 5.4.45 , 5.5.24 - 5.5.35 -----------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(24) "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-----------
object(ReflectionClass)#2 (1) {
["name"]=>
string(0) ""
}
object(ReflectionException)#1 (7) {
["message":protected]=>
string(103) "Class ReflectionClass is an internal class that cannot be instantiated without invoking its constructor"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(9) "/in/hvY2C"
["line":protected]=>
int(27)
["trace":"Exception":private]=>
array(2) {
[0]=>
array(6) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(27)
["function"]=>
string(29) "newInstanceWithoutConstructor"
["class"]=>
string(15) "ReflectionClass"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(15)
["function"]=>
string(46) "instanitateWithoutConstructorThroughReflection"
["args"]=>
array(1) {
[0]=>
string(15) "ReflectionClass"
}
}
}
["previous":"Exception":private]=>
NULL
}
----------------------
-----------ArrayObject-----------
object(ArrayObject)#1 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
object(ReflectionException)#2 (7) {
["message":protected]=>
string(99) "Class ArrayObject is an internal class that cannot be instantiated without invoking its constructor"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(9) "/in/hvY2C"
["line":protected]=>
int(27)
["trace":"Exception":private]=>
array(2) {
[0]=>
array(6) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(27)
["function"]=>
string(29) "newInstanceWithoutConstructor"
["class"]=>
string(15) "ReflectionClass"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(15)
["function"]=>
string(46) "instanitateWithoutConstructorThroughReflection"
["args"]=>
array(1) {
[0]=>
string(11) "ArrayObject"
}
}
}
["previous":"Exception":private]=>
NULL
}
----------------------
Output for 5.4.29 -----------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(24) "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-----------
object(ReflectionClass)#2 (1) {
["name"]=>
string(0) ""
}
object(ReflectionException)#1 (7) {
["message":protected]=>
string(103) "Class ReflectionClass is an internal class that cannot be instantiated without invoking its constructor"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(9) "/in/hvY2C"
["line":protected]=>
int(27)
["trace":"Exception":private]=>
array(2) {
[0]=>
array(6) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(27)
["function"]=>
string(29) "newInstanceWithoutConstructor"
["class"]=>
string(15) "ReflectionClass"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(15)
["function"]=>
string(46) "instanitateWithoutConstructorThroughReflection"
["args"]=>
array(1) {
[0]=>
string(15) "ReflectionClass"
}
}
}
["previous":"Exception":private]=>
NULL
}
----------------------
-----------ArrayObject-----------
Warning: Erroneous data format for unserializing 'ArrayObject' in /in/hvY2C on line 21
Notice: unserialize(): Error at offset 22 of 23 bytes in /in/hvY2C on line 21
bool(false)
object(ReflectionException)#2 (7) {
["message":protected]=>
string(99) "Class ArrayObject is an internal class that cannot be instantiated without invoking its constructor"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(9) "/in/hvY2C"
["line":protected]=>
int(27)
["trace":"Exception":private]=>
array(2) {
[0]=>
array(6) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(27)
["function"]=>
string(29) "newInstanceWithoutConstructor"
["class"]=>
string(15) "ReflectionClass"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(9) "/in/hvY2C"
["line"]=>
int(15)
["function"]=>
string(46) "instanitateWithoutConstructorThroughReflection"
["args"]=>
array(1) {
[0]=>
string(11) "ArrayObject"
}
}
}
["previous":"Exception":private]=>
NULL
}
----------------------
Output for 5.3.0 - 5.3.29 -----------Foo-----------
object(Foo)#1 (0) {
}
----------------------
-----------StdClass-----------
object(stdClass)#1 (0) {
}
----------------------
-----------Bar-----------
object(__PHP_Incomplete_Class)#1 (1) {
["__PHP_Incomplete_Class_Name"]=>
string(3) "Bar"
}
----------------------
-----------ReflectionClass-----------
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
object(ArrayObject)#1 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
----------------------
Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 -----------Foo-----------
object(Foo)#1 (0) {
}
----------------------
-----------StdClass-----------
object(stdClass)#1 (0) {
}
----------------------
-----------Bar-----------
object(__PHP_Incomplete_Class)#1 (1) {
["__PHP_Incomplete_Class_Name"]=>
string(3) "Bar"
}
----------------------
-----------ReflectionClass-----------
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
object(ArrayObject)#1 (0) {
}
----------------------
Output for 5.0.5 -----------Foo-----------
object(Foo)#1 (0) {
}
----------------------
-----------StdClass-----------
object(stdClass)#1 (0) {
}
----------------------
-----------Bar-----------
object(__PHP_Incomplete_Class)#1 (1) {
["__PHP_Incomplete_Class_Name"]=>
string(3) "Bar"
}
----------------------
-----------ReflectionClass-----------
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
object(ArrayObject)#1 (0) {
}
----------------------
Fatal error: Interface 'Serializable' not found in /in/hvY2C on line 35
Process exited with code 255 . Output for 5.0.3 - 5.0.4 -----------Foo-----------
object(Foo)#1 (0) {
}
----------------------
-----------StdClass-----------
object(stdClass)#1 (0) {
}
----------------------
-----------Bar-----------
object(__PHP_Incomplete_Class)#1 (1) {
["__PHP_Incomplete_Class_Name"]=>
string(3) "Bar"
}
----------------------
-----------ReflectionClass-----------
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
----------------------
-----------ArrayObject-----------
object(ArrayObject)#1 (0) {
}
----------------------
Fatal error: Class 'Serializable' not found in /in/hvY2C on line 35
Process exited with code 255 . Output for 5.0.0 - 5.0.2 -----------Foo-----------
object(Foo)#1 (0) {
}
----------------------
-----------StdClass-----------
object(stdClass)#1 (0) {
}
----------------------
-----------Bar-----------
object(__PHP_Incomplete_Class)#1 (1) {
["__PHP_Incomplete_Class_Name"]=>
string(3) "Bar"
}
----------------------
-----------ReflectionClass-----------
object(ReflectionClass)#1 (0) {
}
----------------------
-----------ArrayObject-----------
object(ArrayObject)#1 (0) {
}
----------------------
Fatal error: Class 'Serializable' not found in /in/hvY2C on line 35
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected '{' in /in/hvY2C on line 25
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected '{' in /in/hvY2C on line 25
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error in /in/hvY2C on line 25
Process exited with code 255 . preferences:dark mode live preview
238.91 ms | 401 KiB | 339 Q