Re: Internal Serializable object. Hack to create an object without constructor

From: Date: Sat, 14 Jun 2014 07:40:19 +0000
Subject: Re: Internal Serializable object. Hack to create an object without constructor
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-74898@lists.php.net to get a copy of this message
On Thu, Jun 12, 2014 at 1:48 PM, Remi Collet <remi@fedoraproject.org> wrote: > 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. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > I'm +1 on this change, would like to hear what others think. ps: small typo in the patch: Check that SplObjectStorage::unserialize desn't throws exception when NULL passed should be Check that SplObjectStorage::unserialize doesn't throw exception when NULL passed imo -- Ferenc Kovács @Tyr43l - http://tyrael.hu

« previous php.internals (#74898) next »