> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP iterable

`iterable` is a built-in pseudo-type in PHP, introduced in version 7.1, that acts as a strict type alias for the union `array|Traversable`. While plain objects (such as `stdClass` or user-defined classes) can be iterated over using a `foreach` loop to access visible properties, they do not satisfy the `iterable` type constraint. A value is only considered `iterable` if it is a primitive `array` or an object that explicitly implements the `Traversable` interface.

Because `Traversable` is an internal engine interface that cannot be implemented directly in userland PHP, an object satisfies the `iterable` pseudo-type if it implements either the `Iterator` or `IteratorAggregate` interfaces.

## Type Declarations

`iterable` can be used as a parameter type, a return type, and (as of PHP 7.4) a class property type.

**Parameter Type:**

```php theme={"dark"}
function process(iterable $collection): void {
    foreach ($collection as $key => $value) {
        // Iteration logic
    }
}
```

**Return Type:**

```php theme={"dark"}
function generateCollection(): iterable {
    return new ArrayIterator(['a', 'b', 'c']);
}
```

**Property Type:**

```php theme={"dark"}
class CollectionHolder {
    public iterable $items;
}
```

## Runtime Type Checking

PHP provides the `is_iterable()` function to verify at runtime whether a variable's contents satisfy the `iterable` pseudo-type constraints.

```php theme={"dark"}
var_dump(is_iterable([1, 2, 3]));                           // bool(true)
var_dump(is_iterable(new ArrayIterator([1, 2, 3])));        // bool(true)
var_dump(is_iterable((function() { yield 1; })()));         // bool(true)
var_dump(is_iterable(new stdClass()));                      // bool(false)
```

## Generators and Iterable

Generator functions, which utilize the `yield` keyword, return a `Generator` object. Because the `Generator` class implements the `Iterator` interface, generator return values inherently satisfy the `iterable` type declaration.

```php theme={"dark"}
function getValues(): iterable {
    yield 1;
    yield 2;
    yield 3;
}
```

## Variance Rules in Inheritance

When extending classes or implementing interfaces, `iterable` adheres to PHP's standard rules for type variance.

**Covariance (Return Types):**
A child class can narrow a return type from `iterable` to a more specific type, such as `array` or a specific class implementing `Traversable`.

```php theme={"dark"}
interface DataSource {
    public function read(): iterable;
}

class ArrayDataSource implements DataSource {
    // Valid covariance: narrowing iterable to array
    public function read(): array {
        return [1, 2, 3];
    }
}
```

**Contravariance (Parameter Types):**
A child class can widen a parameter type from `array` or a specific `Traversable` implementation to the broader `iterable` pseudo-type.

```php theme={"dark"}
class BaseProcessor {
    public function process(array $data): void {}
}

class AdvancedProcessor extends BaseProcessor {
    // Valid contravariance: widening array to iterable
    public function process(iterable $data): void {}
}
```

## Default Values

When used as a parameter type, `iterable` allows for default values. This can be an `array`, `null` (if the type is nullable), or, as of PHP 8.1, an instantiated `Traversable` object utilizing `new` expressions in initializers.

```php theme={"dark"}
// Valid: Array default
function executeWithArray(iterable $data = []): void {}

// Valid: Nullable default
function executeNullable(?iterable $data = null): void {}

// Valid (PHP 8.1+): Instantiated object default
function executeWithObject(iterable $data = new ArrayIterator([])): void {}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
