Today we will learn something about the Traversable interface in this Traversable interface in PHP tutorial.
- Traversable interface is an abstract interface and it cannot be used alone.
- It is used to check whether a class is traversable using foreach.
- The traversable interface has no methods defined in it. it is shown below:
- It is empty; hence it is just used as the base class for all the other traversable classes.
- This traversable interface is a base interface for classes that implements it viz. IteratorAggregate and Iterator.
- In the scripts where you use foreach loop, it is automatically implemented by the PHP engine.
- But if you want to implement it in your own class, you need to implement either IteratorAggregate or Iterator which actually extends traversable interface to make your class traversable.
- You can apply your own logic to return values when an object is iterated over.
- When do we face the need to make our class traversable? Generally we use simple foreach loop to access and traverse values in our class, but a foreach loop can only traverse through public values and objects. If you encapsulate an object with private and protected values and write getter and setter methods to access it, it will be problematic for foreach loop to access it. So here to access these values you can make your class to implement IteratorAggregate or Iterator to make your class traversable and write the logic to access these values on your own.
- From the above discussion we understood that to use traversable interface we will have to use IteratorAggregate or Iterator in the implements clause of our class.
- So let us see how it is used.
- Implementing IteratorAggregate:
- IteratorAggregate is very easy to use.
- When your class implements IteratorAggregate it needs to implement one method called getIterator().
- This method is defined to return a data structure that can be iterated over and the PHP engine automatically calls this method when the object is iterated in the foreach loop.
- Let see a demo example of implementation of IteratorAggregate: See the code below:
- In the above example we declared a class hillstations which implements IteratorAggregate. This makes the class traversable.
- We have an array of places and a getIterator() method is defined to return the value.
- Some more functions to set the value or some other task are defined.
- Here the main thing is to implement IeratorAggregate and define getIterator() function.
- You can use IteratorAggregate when you just have to traverse through an array or an object, but if there is any complicated task, you have to use Iterator in the implements clause in the class.
- Implementing Iterator:
- If you want to do some complex task like changing values or examining values before returning them, you need to implement Iterator.
- Iterator is bit more complicated than IteratorAggregate.
- It implements 5 functions viz. current(), key(), next(), rewind() and valid().
- The current() function should return the current value in the iteration.
- The key() function should return the key value of the current element.
- The function next() should advance the iterator forward by one step.
- The rewind() function should reset the iterator and valid() function should return a Boolean value to indicate if the value exists for the current iteration.
- The PHP engine automatically calls these functions while traversing the values using foreach loop.
- Let us see a demo example implementing Iterator:
- Here, we can see that the class hillstations implements Iterator.
- We have a private array called places and a counter initialized to 0.
- The five functions of interface are also implemented.
- These functions will be automatically called while traversing the data using foreach loop.
- The most important advantage of Iterator over IteratorAggregate is to manipulate the data before returning it.
class hillstations implements IteratorAggregate
public function getIterator()
// some more functions here
} // end of class
class hillstations implements Iterator
public function current()
//return the current value
public function next()
// increment the counter
public function rewind()
// reset the counter to 0
public function key()
//return the key value of the current value
public function valid()
//return the Boolean value to indicate if the value exists
// some more functions here
We generally use IteratorAggregate and Iterator to implement traversable interface, but we can use traversable while checking if a particular class is traversable or not or a particular object is an instance of a traversable class.
Thus we understood the traversable interface in this Traversable interface in PHP tutorial.