0

I am iterating an ArrayObject inside of the object itself. Lets see:

class EventsBag extends ArrayObject{

    public function toJson(){
        $events = [];
        $iterator = $this->getIterator();

        while ($iterator->valid()){
            $events[] = $iterator->current();
            $iterator->next();
        }

        return $events;
    }
}

Calling toJson() never ends the script execution:

$events = new EventsBag(['a', 'b']);
var_dump($events->toJson());

I have debugged the script, and the while control never get out. I do not get why this happends.

manix
  • 14,537
  • 11
  • 70
  • 107
  • Perhaps because there's a typo at `$iterator->currect`. (See here for [displaying PHP errors](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)). – Kenney Nov 29 '15 at 01:41
  • Hello @Kenney. Typos may occurred in the transcription, but that is not the case. – manix Nov 29 '15 at 01:43
  • Works fine here - https://3v4l.org/mXqHi. Is this your actual code, or just an abstract/filtered code block? – Sean Nov 29 '15 at 01:44
  • @Sean, I have just filtered 3 lines decorating printing some values, but this code is not running in my machine. I have seen yours and surprise me that the same lines works for you. – manix Nov 29 '15 at 01:48

0 Answers0