Internal Serializable object. Hack to create an object without constructor
| From: | Remi Collet | Date: | Thu, 12 Jun 2014 11:44:44 +0000 |
| Subject: | Internal Serializable object. Hack to create an object without constructor | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-74866@lists.php.net to get a copy of this message | ||
Hi,
As Internal Class doesn't support newInstanceWithoutConstructor, some
user code use the above hack (ex phpunit-mock-object)
$object = unserialize(
sprintf('%s:%d:"%s":0:{}',
(version_compare(PHP_VERSION, '5.4', '>')
&& $class->implementsInterface("Serializable") ? "C" :
"O"),
strlen($className),
$className
)
);
Of course, this is already a hack.
Of course, better solution will be to support
newInstanceWithoutConstructor for all internals classes, but this is
another story
Problem, Serializable internal class raised the
'Empty serialized string cannot be empty' exception.
I don't really see any reason to make serialized string mandatory, as
those classes can be create (constructor) without any mandatory option.
The trivial attached patch could make everyone life easier.
The new test serialize again the result object to ensure all internal
structures are properly initialized, and no segfault.
Feedback welcome.
Remi.