php8.5
Home/ Manual/ reflection / reflectionclass/ ReflectionClass::isIterable

ReflectionClass::isIterable

PHP function Edit on GitHub ✎

(PHP 7 >= 7.2.0, PHP 8)

Check whether this class is iterable

Description

ReflectionClass::isIterable(): bool

Check whether this class is iterable (i.e. can be used inside Foreach).

Parameters Parameters

Return Values

Returns true if the class is iterable or false otherwise.

Examples

Basic ReflectionClass::isIterable Usage

php
<?php

class IteratorClass implements Iterator
{
    public function __construct() {}

    public function key(): mixed {}

    public function current(): mixed {}

    public function next(): void {}

    public function valid(): bool {}

    public function rewind(): void {}
}

class DerivedClass extends IteratorClass {}

class NonIterator {}

function dump_iterable($class)
{
    $reflection = new ReflectionClass($class);
    var_dump($reflection->isIterable());
}

$classes = ["ArrayObject", "IteratorClass", "DerivedClass", "NonIterator",];

foreach ($classes as $class) {
    echo "Is $class iterable? ";
    dump_iterable($class);
}
?>

The above example will output:

output
Is ArrayObject iterable? bool(true)
Is IteratorClass iterable? bool(true)
Is DerivedClass iterable? bool(true)
Is NonIterator iterable? bool(false)

See Also

  • ReflectionClass::__construct

Source: reference/reflection/reflectionclass/isiterable.xml · from the official PHP manual (php/doc-en)