Req #62200 [Opn->Csd]: ArrayObject should implement __set_state()

From: Date: Fri, 07 Feb 2020 06:07:14 +0000
Subject: Req #62200 [Opn->Csd]: ArrayObject should implement __set_state()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225406@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62200&edit=1 ID: 62200 Updated by: phpdocbot@php.net Reported by: bkam at citiesunlimited dot com Summary: ArrayObject should implement __set_state() -Status: Open +Status: Closed Type: Feature/Change Request Package: SPL related Operating System: All PHP Version: 5.4.3 Block user comment: N Private report: N New Comment: Automatic comment on behalf of cmb Revision: http://git.php.net/?p=doc/en.git;a=commit;h=ec77057bf375ae4d50ffe24aa6f6a5dc629c10f3 Log: Fix #62200: __set_state() not implemented Previous Comments: ------------------------------------------------------------------------ [2016-06-29 17:04:27] cmb@php.net I have fixed the documentation, and change this ticket to feature request. ------------------------------------------------------------------------ [2016-06-29 17:03:36] cmb@php.net Automatic comment from SVN on behalf of cmb Revision: http://svn.php.net/viewvc/?view=revision&revision=339526 Log: Fix #62200: __set_state() not implemented ------------------------------------------------------------------------ [2012-06-05 00:11:56] bkam at citiesunlimited dot com For this class, that's not an acceptable position to take. Consider that I can write: class AdaptorArrayObject extends \ArrayObject { public static function __set_state(Array $data) { $this->exchangeArray($data); } } //::then:: //some data that's structured appropriately for ArrayObject state $oldArrayObjectData = $existingArrayObject->getArrayCopy(); $newArrayObject = AdaptorArrayObject::__set_state($oldArrayObjectData); You already wrote the functions necessary to implement __set_state() Why is it on ME to subclass just to call the functions to make this work? Functions are written, call them internally and expose __set_state. Easy peasy. The other SPL data structures are indeed much more complex internally (object keys, kv meta data, and value linking), so it makes sense not to offer external functions like exchangeArray or getArrayCopy. But ArrayObject is supposed to be much closer to [] is it not? just wrapped in object reference semantics and SPL API goodies? Please reconsider for ArrayObject. ------------------------------------------------------------------------ [2012-06-04 23:54:00] johannes@php.net This should be cleared in the documentation. The code returned is valid PHP code, while it won't necessarily run successful. var_export() assumes a __set_state() exists, but there are cases where it doesn't and there are cases where for different reasons it never can in a sufficient way. Especially for internal classes where we can't export the full state. ------------------------------------------------------------------------ [2012-05-31 23:50:43] bkam at citiesunlimited dot com Description: ------------ Pretty simple, var_export of SPL traversibles shows Class::__set_state([]) but actually calling that function throws an undefined method error. This is specifically true for ArrayObject, but generally true for all the traversibles with inaccessible internal structures. Test script: --------------- //first $a = new ArrayObject(); $a['key'] = 'value'; var_export($a); // returns //"ArrayObject::__set_state(array( 'key' => 'value', ))" $ao = ArrayObject::__set_state(['thing' => 'value']); //returns //"Fatal error: Call to undefined method ArrayObject::__set_state()" Expected result: ---------------- "ArrayObject::__set_state(array( 'key' => 'value', ))" "" Actual result: -------------- "Fatal error: Call to undefined method ArrayObject::__set_state()" ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62200&edit=1

« previous php.bugs (#225406) next »