|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-05-31 23:50 UTC] 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()" PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 20:00:01 2025 UTC |
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.