9

I Just heard about Iterables from PHP 7.1 docs. But didn't got its actual use case and neither the concept is clear for me. so can anyone explain it with some easy example to grab it faster?

I want to know why and where we use it? What are the benefits of iterable?

Naveen Giri
  • 535
  • 6
  • 17
  • but why we need that? – Naveen Giri May 03 '17 at 13:14
  • But Jon, we can do it with foreach too. why we need iterable to do so? – Naveen Giri May 03 '17 at 13:19
  • I am not getting what you trying to explain. but my question is when I want to make anything to get integrate I use array or object but iterator concept is quite new for me. It came in 7.1 so I want to know what its need? where we need it? – Naveen Giri May 03 '17 at 13:23
  • I am talking about this http://php.net/manual/en/language.types.iterable.php – Naveen Giri May 03 '17 at 13:25
  • 1
    This might help https://wiki.php.net/rfc/iterable The main advantage of having `iterable` is that a class, method or function parameter can be declared `iterable` but not be concerned of the implementation ie array, Iterator, Generator, etc. So anything that is iterable can be used. – nerdlyist May 03 '17 at 13:32
  • Ah, so the basics is that you'd use the type for e.g. hinting when you want to accept an array (primitive) or an object (implementing Traversable), whereas before, you could only do one or the other even though in many cases they can be treated the same. – Jonnix May 03 '17 at 13:32
  • (Deleted previous comments as I had the wrong end of the question stick.) – Jonnix May 03 '17 at 13:34

2 Answers2

3

This might help wiki.php.net/rfc/iterable

The main advantage of having iterable is that a class, method or function parameter can be declared iterable but not be concerned of the implementation ie array, Iterator, Generator, etc. So anything that is iterable can be used.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
2

I am a beginner in PHP and for the last 5 hours I have been researching on the subject. As I understand it, an iterator allows us to read large amounts of data that happen to overflow the RAM memory, an example would be an analysis of large log files.

By using a mode called lazy in nature that reads one element at a time, you could read a 4GB file without having to load it all into memory. In theory, you could process an infinite amount of information.

For that we need to define some methods:

<?php
    class myIterator implements Iterator {
        private $position = 0;
        private $array = array("A","B","C",);

        public function __construct() {
            $this->position = 0;
        }
        function rewind() {
            $this->position = 0;
        }
        function current() {
            return $this->array[$this->position];
        }
        function key() {
            return $this->position;
        }
        function next() {
            $this->position++;
        }
        function valid() {
            return isset($this->array[$this->position]);
        }
    }

    $it = new myIterator;

    foreach($it as $key => $value) {
        var_dump($key, $value);
        echo "\n";
    }

In PHP 7.1 this can be reduced to:

<?php
    class myIterable {
        function foo(iterable $iterable){
            foreach ($iterable as $key => $value) {
                var_dump($key, $value);
                echo "\n";
            }
        }
    }
    $it = new myIterable;
    $it->foo(array("A","B","C",));

Hope this helps ;)