Iterables in PHP

I
Namespaces in PHP
MySQL in PHP

An iterable is a value which is used in loops.

It is a great solution if you want to keep track on how often you went
through a loop. For example if you want to break out of a loop after
ten times you can easily do this.

  • An iterable is any value which is accepted by a foreach loop and, of course, is iterable.
  • PHP 7.1 introduced this pseudo-data type and it can be used as return data type of functions. Also, the function can accept iterables as its argument.

How to use Iterables in PHP?

The iterable keyword defines the type of the argument or return type of the function should be an iterable value. Look at the following example.

<?php
function printIterable(iterable $myIterable) {
  foreach($myIterable as $item) {
    echo $item;
  }
}

$arr = ["a", "b", "c"];
printIterable($arr);
?>
  • In the above example. we create a function that accepts some argument. We specify the argument must be iterable using the iterable keyword.
  • Now, if we call this function and pass the non-iterable value as the argument of the function, it will throw an error.
  • So, iterables make sure to feed an iterable value to the foreach loop inside a function. Think and try to pass non-iterable value to the foreach loop and check what happens. Then, you should be more clear about the use of iterables.

Example – Function returning Iterable

In the above example you saw how to use an iterable as an argument of a function. Next we will see how to use an iterable as the return value.

<?php
function getIterable():iterable {
  return ["a", "b", "c"];
}

$myIterable = getIterable();
foreach($myIterable as $item) {
  echo $item;
}
?>
  • In the above example, we create a function that simply returns some value. Focus on the return type of the function on the right side of the function name iterable. It makes it mandatory for the function to return an iterable value.
  • Call the function and pass the returned value to the foreach loop. Try to run this example on your machine and see what echo prints on the screen.
  • Try to practice this example by returning some non-iterables values out of the function. Read the error and then return some single and two-dimensional arrays.

How to create Iterables in PHP?

  • Arrays: All arrays in PHP are iterables. As at the beginning of this read, you learned that any data that foreach loop can accept is iterable. So, all the single, multi-dimensional, and associative arrays are iterables by default.
  • Iterators: An object implemented iterator interface is also accepted by a function that requires iterable data in an argument.

An iterator consists of the data to loop on it and methods that work together to loop through the data. A pointer always points at an element of the data. Each element of the data contains a key that is used to find each element inside the data of the iterable.

To make an iterator, it should implement the following methods.

  • next() – Moves the pointer to the next element in the data.
  • rewind() – The pointer starts pointing at the first element of data.
  • valid() – It makes sure that the pointer is pointing to its desired element or not. For example, if the next() is called and the pointer is at the last element of the data i:e no data element exists after it. Then valid will return false. It will return true when the next() moves to the next element and rewind() moves to the first element.

Example

Creating iterable by implementing an iterator interface and then using it as an iterable.

<?php
// Create an Iterator
class MyIterator implements Iterator {
  private $items = [];
  private $pointer = 0;

  public function __construct($items) {
    // array_values() makes sure that the keys are numbers
    $this->items = array_values($items);
  }

  public function current() {
    return $this->items[$this->pointer];
  }

  public function key() {
    return $this->pointer;
  }

  public function next() {
    $this->pointer++;
  }

  public function rewind() {
    $this->pointer = 0;
  }

  public function valid() {
    // count() indicates how many items are in the list
    return $this->pointer < count($this->items);
  }
}

// A function that uses iterables
function printIterable(iterable $myIterable) {
  foreach($myIterable as $item) {
    echo $item;
  }
}

// Use the iterator as an iterable
$iterator = new MyIterator(["a", "b", "c"]);
printIterable($iterator);
?>

Now that you became familiar with the basics of PHP it is time to look into MYSQL databases next.

Namespaces in PHP
MySQL in PHP

Tutor Network

Share this

Learn PHP from A to Z