Bug #72899 [Opn->Nab]: Iterating over an ArrayObject does not call offsetGet

From: Date: Fri, 19 Aug 2016 17:08:39 +0000
Subject: Bug #72899 [Opn->Nab]: Iterating over an ArrayObject does not call offsetGet
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203400@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72899&edit=1 ID: 72899 Updated by: requinix@php.net Reported by: c dot tigra at gmail dot com -Summary: tested in https://3v4l.org/ +Summary: Iterating over an ArrayObject does not call offsetGet -Status: Open +Status: Not a bug Type: Bug Package: SPL related Operating System: Linux PHP Version: 7.1.0beta3 Block user comment: N Private report: N New Comment: The Expected and Actual are backwards. https://3v4l.org/G6VW0 It doesn't call offsetGet for two reasons: 1. Because offsetSet (nor the rest of the ArrayAccess interface) is not used for iteration. 2. Because ArrayObject implements IteratorAggregate, which *is* used for iteration. It does so by using ArrayIterator (by default). Sounds like you want to change what values are exposed during iteration? I can't imagine that ever being a good idea, but whatever. Try (a) overriding getIterator with your own iterable values https://3v4l.org/2LCBG or (b) using your own class for iteration https://3v4l.org/7tMHH Previous Comments: ------------------------------------------------------------------------ [2016-08-19 16:46:26] c dot tigra at gmail dot com Description: ------------ offsetGet() don't called if ArrayObject uses in foreach(). It directly returns value from private storge of ArrayObject. Test script: --------------- class Some extends ArrayObject { function __construct(){ parent::__construct(array("key"=>"storged value"),ArrayObject::ARRAY_AS_PROPS); } function offsetGet($index){ return "offsetGet called"; } } $obj=new Some(); print $obj->key."<br>"; foreach($obj as $key=>$val){ print $val."<br>"; } Expected result: ---------------- offsetGet called storged value Actual result: -------------- offsetGet called offsetGet called ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72899&edit=1

« previous php.bugs (#203400) next »