|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-12-29 13:19 UTC] jani@php.net
-Package: Feature/Change Request
+Package: SPL related
-Operating System: Linux / Ubuntu 9.04
+Operating System: *
-PHP Version: 5.3.0
+PHP Version: *
[2020-11-18 13:03 UTC] cmb@php.net
-Status: Open
+Status: Suspended
[2020-11-18 13:03 UTC] cmb@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 07:00:01 2025 UTC |
Description: ------------ It would be nice if you could affect the behaviour of casting an ArrayObject to an array by overloading the getArrayCopy() method. This would be usefull to create lazy load arrays. Reproduce code: --------------- <?php class Items extends ArrayObject { /** * Stub for loading an item. * @return object */ public function load($id) { return (object)(array('id'=>$id)); } public function offsetGet($key) { $item = parent::offsetGet($key); if (!is_object($item)) { $item = $this->load($item); $this->offsetSet($key, $item); } return $item; } public function getArrayCopy() { $array = parent::getArrayCopy(); foreach ($array as $i=>&$item) { if (!is_object($item)) { $item = $this->load($item); $this->offsetSet($i, $item); } } return $array; } } $a = new Items(); $a[] = 'jan'; $a[] = 'piet'; var_dump($a[0]); var_dump((array)$a); Expected result: ---------------- object(stdClass)#2 (1) { ["id"]=> string(3) "jan" } array(2) { [0]=> object(stdClass)#2 (1) { ["id"]=> string(3) "jan" } [1]=> object(stdClass)#3 (1) { ["id"]=> string(4) "piet" } } Actual result: -------------- object(stdClass)#2 (1) { ["id"]=> string(3) "jan" } array(2) { [0]=> object(stdClass)#2 (1) { ["id"]=> string(3) "jan" } [1]=> string(4) "piet" }