What is an Iterable?
In PHP, an iterable is any value that can be looped through using the foreach() loop. This includes arrays and objects that implement the Iterator interface. Iterables are introduced as a pseudo-type in PHP 7.1, and they can be used for function arguments and return types.
Why Use Iterables?
Generalization:
You can pass any iterable (arrays or objects that implement the Iterator interface) to a function, making your code more flexible and generalized.
Convenience:
You can easily loop over elements in an iterable using the foreach() loop without worrying about the internal structure of the data.
Using Iterables in Functions
You can specify iterable as a type for both function arguments and return types.
Example 1: Iterable Function Argument
<?php function printIterable(iterable $myIterable) { foreach ($myIterable as $item) { echo $item; } } $arr = ["a", "b", "c"]; printIterable($arr); // Output: abc ?>
In this example, the function printIterable() accepts an iterable ($myIterable) and loops through its elements using foreach().
Example 2: Return an Iterable
<?php function getIterable(): iterable { return ["a", "b", "c"]; } $myIterable = getIterable(); foreach ($myIterable as $item) { echo $item; } ?>
In this case, the getIterable() function returns an iterable, which is then looped through in the foreach() loop.
Creating Iterables
There are two common types of iterables in PHP:
Arrays:
All arrays are inherently iterable. You can pass an array to any function that expects an iterable.
Iterators:
Objects that implement the Iterator interface can also be used as iterables.
Implementing an Iterator
To create a custom iterable object, you can implement the Iterator interface. This interface requires you to define the following methods:
current(): Returns the current element.
key(): Returns the key of the current element.
next(): Moves the pointer to the next element.
rewind(): Resets the pointer to the first element.
valid(): Checks if the current element is valid.
Example: Implementing a Custom Iterator
<?php // Create an Iterator class MyIterator implements Iterator { private $items = []; private $pointer = 0; public function __construct($items) { // Ensuring the keys are numeric $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() { 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); // Output: abc ?>
How It Works:
Iterator Class:
The MyIterator class implements the Iterator interface. It stores an array of items and provides methods to traverse through it.
Using the Iterator:
The printIterable() function works the same way as before, accepting any iterable (including our custom iterator).
Output:
The foreach() loop inside the printIterable() function prints each item in the iterable: a, b, and c.
Conclusion
PHP’s iterable type is useful for writing flexible and reusable functions. You can pass any array or custom iterator to such functions, allowing you to handle data from various sources with a uniform approach. This is especially helpful in cases where you want to work with both simple arrays and complex objects in a consistent way.
By implementing the Iterator interface, you can create your own custom iterable classes to fit your specific needs.