<?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
);
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);
}
}
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";
}
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/AohKP on line 26
Foo
object(Foo)#1 (0) {
}
object(Foo)#2 (0) {
}
StdClass
object(stdClass)#2 (0) {
}
object(stdClass)#1 (0) {
}
Bar
Warning: Erroneous data format for unserializing 'Bar' in /in/AohKP on line 12
Warning: unserialize(): Error at offset 13 of 14 bytes in /in/AohKP on line 12
bool(false)
object(Bar)#2 (1) {
["data":"Bar":private]=>
NULL
}
ReflectionClass
Fatal error: Uncaught Exception: Unserialization of 'ReflectionClass' is not allowed in /in/AohKP:12
Stack trace:
#0 /in/AohKP(12): unserialize('O:15:"Reflectio...')
#1 /in/AohKP(41): instanitateWithoutConstructorThroughUnserialize('ReflectionClass')
#2 {main}
thrown in /in/AohKP on line 12
Process exited with code 255.
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17
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/AohKP on line 26
Foo
object(Foo)#1 (0) {
}
object(Foo)#2 (0) {
}
StdClass
object(stdClass)#2 (0) {
}
object(stdClass)#1 (0) {
}
Bar
Warning: Erroneous data format for unserializing 'Bar' in /in/AohKP on line 12
Notice: unserialize(): Error at offset 13 of 14 bytes in /in/AohKP on line 12
bool(false)
object(Bar)#2 (1) {
["data":"Bar":private]=>
NULL
}
ReflectionClass
Fatal error: Uncaught Exception: Unserialization of 'ReflectionClass' is not allowed in /in/AohKP:12
Stack trace:
#0 /in/AohKP(12): unserialize('O:15:"Reflectio...')
#1 /in/AohKP(41): instanitateWithoutConstructorThroughUnserialize('ReflectionClass')
#2 {main}
thrown in /in/AohKP on line 12
Process exited with code 255.
Output for 7.4.0 - 7.4.33, 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
Warning: Erroneous data format for unserializing 'Bar' in /in/AohKP on line 12
Notice: unserialize(): Error at offset 13 of 14 bytes in /in/AohKP on line 12
bool(false)
object(Bar)#2 (1) {
["data":"Bar":private]=>
NULL
}
ReflectionClass
Warning: Erroneous data format for unserializing 'ReflectionClass' in /in/AohKP on line 12
Notice: unserialize(): Error at offset 26 of 27 bytes in /in/AohKP on line 12
bool(false)
object(ReflectionClass)#1 (1) {
["name"]=>
string(0) ""
}
ArrayObject
Fatal error: Uncaught UnexpectedValueException: Incomplete or ill-typed serialization data in /in/AohKP:12
Stack trace:
#0 [internal function]: ArrayObject->__unserialize(Array)
#1 /in/AohKP(12): unserialize('O:11:"ArrayObje...')
#2 /in/AohKP(41): instanitateWithoutConstructorThroughUnserialize('ArrayObject')
#3 {main}
thrown in /in/AohKP on line 12
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
Warning: Erroneous data format for unserializing 'Bar' in /in/AohKP on line 12
bool(false)
object(Bar)#2 (1) {
["data":"Bar":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/AohKP on line 12
bool(false)
object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}
Foo
object(Foo)#1 (0) {
}
object(Foo)#2 (0) {
}
StdClass
object(stdClass)#2 (0) {
}
object(stdClass)#1 (0) {
}
Bar
Warning: Erroneous data format for unserializing 'Bar' in /in/AohKP on line 12
Notice: unserialize(): Error at offset 13 of 14 bytes in /in/AohKP on line 12
bool(false)
object(Bar)#2 (1) {
["data":"Bar":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/AohKP on line 12
Notice: unserialize(): Error at offset 22 of 23 bytes in /in/AohKP on line 12
bool(false)
object(ArrayObject)#2 (1) {
["storage":"ArrayObject":private]=>
array(0) {
}
}