PHP : Iterables

In PHP, an iterable is a data type that can be iterated over using a foreach loop. It includes arrays and objects that implement the `Traversable` interface. The iterable keyword is a type declaration introduced in PHP 7.1 to indicate that a parameter or return type must be an array or an object that implements `Traversable`.

Iterable Type Declaration:

The iterable type declaration can be used in function/method signatures to specify that a parameter must be an array or an object implementing Traversable. It can also be used as a return type.

Example:


function process(iterable $items) {
    foreach ($items as $item) {
        echo $item . "<br>";
    }
}

// Using the function with an array
$array = [1, 2, 3];
process($array);

// Using the function with an object implementing Traversable
class MyCollection implements IteratorAggregate {
    private $items = [];

    public function __construct(array $items) {
        $this->items = $items;
    }

    public function getIterator() {
        return new ArrayIterator($this->items);
    }
}

$collection = new MyCollection([4, 5, 6]);
process($collection);

 

Iterating Over Iterable Types:

You can use the foreach loop to iterate over iterable types, such as arrays and objects implementing Traversable.

Example:


// Iterating over an array
$array = ['apple', 'banana', 'cherry'];
foreach ($array as $item) {
    echo $item . "<br>";
}

// Iterating over an object implementing Traversable
$collection = new MyCollection(['dog', 'cat', 'rabbit']);
foreach ($collection as $item) {
    echo $item . "<br>";
}

 

Iterable as Return Type:

You can specify iterable as a return type for a function or method that returns an array or an object implementing Traversable.

Example:


function generateItems(): iterable {
    yield 'one';
    yield 'two';
    yield 'three';
}

$items = generateItems();
foreach ($items as $item) {
    echo $item . "<br>";
}

 

Checking if a Variable is Iterable:

You can use the is_iterable() function introduced in PHP 7.1 to check if a variable is an iterable type.

Example:


$var1 = [1, 2, 3];
$var2 = new MyCollection(['a', 'b', 'c']);

var_dump(is_iterable($var1)); // Output: bool(true)
var_dump(is_iterable($var2)); // Output: bool(true)

 

Use Cases:

  • Flexible Function Signatures: When a function can accept both arrays and objects that implement Traversable.
  • Code Reusability: Writing functions that can work with various iterable data structures.
  • Iterator Objects: Creating custom iterator objects that implement Traversable.

Considerations:

  • Array vs. Iterable: While iterable includes arrays, it's a good practice to use array when a function specifically expects an array and iterable when you want to allow both arrays and objects.
  • Performance: Iterating over arrays tends to be faster than iterating over objects, especially large objects.

Conclusion:

In PHP, iterable is a type declaration that specifies that a parameter or return type must be an array or an object implementing Traversable. It is useful when writing functions or methods that can accept various types of iterable data structures, such as arrays and objects. iterable allows for flexible and reusable code that can work with different data sources. It's important to understand when and how to use iterable to create more versatile and maintainable PHP applications.

AI car modification · buy quality backlinks · guest post service · link building service · link building service · buy backlinks · SEO, GEO, AEO, AIO and LLMO · Bedeutung von Backlinks für Google · winandoffice · aiswapface photo face swap · dofollow backlinks · link building service · guest post service · quality backlinks · guest post service · dofollow backlinks · link building service · dofollow backlinks · buy backlinks · buy quality backlinks · dofollow backlinks · dofollow backlinks · link building service · hacklink satın al · Contextual backlinks: judge the paragraph and destination together · hacklink · hacklink satın al · hacklink panel satın al · jojobet · hacklink satın al · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet · jojobet giriş · jojobet güncel · jojobet · jojobet · jojobet · jojobet