Req #49454 [Opn->Sus]: Call ArrayObject::getArrayCopy() when casting an ArrayObject to an array

From: Date: Wed, 18 Nov 2020 13:03:22 +0000
Subject: Req #49454 [Opn->Sus]: Call ArrayObject::getArrayCopy() when casting an ArrayObject to an array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230429@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=49454&edit=1 ID: 49454 Updated by: cmb@php.net Reported by: info at adaniels dot nl Summary: Call ArrayObject::getArrayCopy() when casting an ArrayObject to an array -Status: Open +Status: Suspended Type: Feature/Change Request Package: SPL related Operating System: * PHP Version: * Block user comment: N Private report: N New Comment: This feature would need to be discussed, for what this bug tracker is not suitable. If you, or anybody else, is still interested in this, please forward the request to the internals mailing list. For the time being, I'm suspending this ticket. Previous Comments: ------------------------------------------------------------------------ [2009-09-03 11:55:04] info at adaniels dot nl 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" } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=49454&edit=1

« previous php.bugs (#230429) next »