Skip to main content

PHP OOP Iterables

By SamK
0
0 recommends
Category(s)
Topic(s)

An iterable refers to any value that can be traversed using a foreach() loop.

Introduced in PHP 7.1, the iterable pseudo-type allows it to be used as a data type for both function arguments and return values.

Using Iterables

The iterable keyword can serve as the data type for a function argument or as the return type for a function.

Example: Iterable as a function argument

<?php
function displayItems(iterable $items) {
  foreach ($items as $element) {
    echo $element . " ";
  }
}

$list = ["x", "y", "z"];
displayItems($list);

/*
Output:
x y z
*/
?>

Example: Iterable as a return type

<?php
function fetchItems(): iterable {
    return ["x", "y", "z"];
}

$items = fetchItems();
foreach ($items as $value) {
    echo $value . " ";
}

/*
Output:
x y z
*/
?>

Iterables Types

  • Arrays: All arrays in PHP are inherently iterables, which means they can seamlessly be passed as arguments to functions expecting an iterable type.
  • Iterators: Any object implementing the Iterator interface qualifies as an iterable and can also be passed to functions that require an iterable parameter. An iterator represents a collection of items and includes methods to iterate through the collection. It maintains a pointer that tracks the current item within the collection. Each item has an associated key that can be used to access it.

Iterables vs Arrays

  • Definition: Arrays in PHP are ordered collections of values, which can be indexed or associative. While iterables, such as generators, are objects that can be looped over, often with lazy evaluation.
  • Memory: Arrays store all elements in memory at once, making them inefficient for large datasets. While iterables do not load all data into memory; they generate values one at a time, making them more memory-efficient.
  • Use Case: Arrays are Best for small to medium datasets where quick access to elements is needed. While iterables are Ideal for large datasets, databases, or scenarios where processing data on-the-fly is required.
  • Modification: Arrays are mutable and can have elements added, modified, or removed easily. While iterables can be looped through and processed, they can't be modified directly in the same way arrays can.

In summary, use arrays for simple, small datasets and iterables (like iterators) for large, memory-sensitive tasks.

An Iterator Example

An iterator must implement the following methods:

  • current() - Returns the element the pointer currently references. The return type can be of any data type.
  • key() - Returns the key associated with the current element. The key can only be an integer, float, boolean, or string.
  • next() - Moves the pointer to the next element in the collection.
  • rewind() - Resets the pointer to the first element in the collection.
  • valid() - Checks if the pointer references a valid element. Returns false when the pointer is outside the bounds of the collection (e.g., after the last element). Returns true in all other cases.

Example Code:

<?php
// Create a custom Iterator
class CarsIterator implements Iterator {
  private $cars = [];
  private $pointer = 0;

  public function __construct($cars) {
    // Ensure the cars are indexed with numeric keys
    $this->cars = array_values($cars);
  }

  public function current() {
    // Return the current car based on the pointer
    return $this->cars[$this->pointer];
  }

  public function key() {
    // Return the current key (pointer value)
    return $this->pointer;
  }

  public function next() {
    // Move the pointer to the next element
    $this->pointer++;
  }

  public function rewind() {
    // Reset the pointer to the first element
    $this->pointer = 0;
  }

  public function valid() {
    // Check if the pointer is within the bounds of the array
    return $this->pointer < count($this->cars);
  }
}

// Function to print elements from any iterable
function printCars(iterable $carsIterable) {
  foreach($carsIterable as $car) {
    // Print each car
    echo $car . " ";
  }
}

// Create an iterator object and pass it to the printCars function
$cars = new CarsIterator(["Lancer", "Corolla", "Civic"]);
printCars($cars);

/*
Output:
Lancer Corolla Civic
*/
?>

Questions & Answers