Skip to content

throw error when iterating over closure #16823

@BenKucenski

Description

@BenKucenski

Description

The foreach should also throw a fatal error "PHP Fatal error: Uncaught Error: Cannot use object of type Closure as array"

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$test = [
    'a' => 1,
    'b' => function() {
        echo 'called' . PHP_EOL;
        return [1, 2, 3];
    }
];

echo '1' . PHP_EOL;
print_r($test);

foreach($test['b'] as $item) { // no error reported
    print_r($item);
}

print_r($test['b'][0]); // error reported

echo '2' . PHP_EOL;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions