Search code examples
phpiteratorsplarrayaccess

PHP, SPL, AccessArray Interface with Iterator Interface


I was thinking about AccessArray interface and the Iterator interface and i was thinking can we combine them?, I mean Setting up array with AccessArray and use a foreach loop with the Iterator, If can some one please help me with example if it possible to combine those two.


Solution

  • Note that you are basically asking for ArrayObject.

    class Foo extends ArrayObject
    {
      private $data = [];
      public function __construct()
      {
        parent::__construct($this->data);
      }
    }