PHP Velho Oeste 2024

iterable

Iterable (yinelenebilir tür) yerleşik olarak derleme sırasında array|Traversable türüne bir takma ad olarak tanımlanmıştır. PHP 7.1.0'dan PHP 8.2.0'a kadar yukarıda belirtilen tür takma adı olarak işlev gören ve bir tür bildirimi olarak kullanılabilen yerleşik bir sözde türdü. Yinelenebilir tür, üreteç içinde yield from ile ve foreach içinde kullanılabilir.

Bilginize:

Dönüş türü olarak iterable bildiren bir işlev ayrıca, bir üreteç olarak da kullanılabilir.

Örnek 1 - Yinelenebilir üreteç dönüş türü örneği

<?php

function gen(): iterable {
yield
1;
yield
2;
yield
3;
}

?>

add a note add a note

User Contributed Notes 1 note

up
-13
j_jaberi at yahoo dot com
4 years ago
Just to note:
Though objects may (or may not) be Traversable, the can use in foreach because implicit conversion to array
<?php
class Foo {
    public
$a = 1;
    public
$b = "Helo";
};

$bar = new Foo;

foreach(
$bar as $elm) {
    echo
$elm . ' ';
}

?>
prints 1 Hello
Even
<?php
$bar
= new stdClass
foreach($bar as $elm) {
    echo
$elm . ' ';
}
?>
is correct.
To Top