← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 20 of 24

PHP Iterables

"चीज़ों के collection" पर loop चलाने वाला एक function अक्सर actually परवाह नहीं करता कि वह collection एक plain array है या loop होने के लिए बनाया गया एक custom object -- दोनों foreach से चले जा सकते हैं। PHP का iterable type hint exactly इसी idea को capture करता है: यह एक array या कोई भी Traversable object accept करता है, एक function को बिना extra काम के दोनों accept करने देते हुए।
Syntax
php
function function_name(iterable $items): iterable {
    foreach ($items as $item) {
        yield $item;
    }
}

is_iterable($value);

क्या Iterable Count होता है

दो तरह की values iterable type satisfy करती हैं: कोई भी plain PHP array, और कोई भी object जो built-in Traversable interface implement करता हो (जिसमें Iterator interface और Generator functions दोनों शामिल हैं) -- दोनों एक foreach loop का target हो सकते हैं।

उदाहरण: What Counts as Iterable

php
<?php
// Define the function `gen` with no parameters
function gen() {
    yield 1;
    yield 2;
}
// Print a detailed dump (with types) of `is_iterable([1, 2, 3])`
var_dump(is_iterable([1, 2, 3]));
// Print a detailed dump (with types) of `is_iterable(gen())`
var_dump(is_iterable(gen()));
?>

Parameter Type की तरह iterable इस्तेमाल करना

किसी function parameter को array के बजाय iterable type-hint करना signal करता है कि function input पर सिर्फ foreach से loop चलाएगा, और इसे specifically एक real array होने की माँग नहीं है -- function को inputs की एक wider range के साथ usable बनाते हुए, बड़े generators सहित।

उदाहरण: Using iterable as a Parameter Type

php
<?php
// Define the function `printAll` taking `iterable $items`
function printAll(iterable $items) {
    // Loop over `$items`, binding each item to `$item`
    foreach ($items as $item) {
        // Print `$item . "\n"` to the output
        echo $item . "\n";
    }
}
// Call `printAll([1, 2, 3])`
printAll([1, 2, 3]);
?>

Return Type की तरह iterable इस्तेमाल करना

एक function अपना return type भी iterable declare कर सकता है, यह signal करते हुए कि यह कुछ loopable return करेगा -- या तो एक real array जो यह बनाता है, या yield इस्तेमाल करने वाला एक generator -- बिना यह commit किए कि कौन सा specific one, function की implementation को बाद में बदलने की flexibility देते हुए।

उदाहरण: Using iterable as a Return Type

php
<?php
// Define the function `numbers` with no parameters
function numbers(): iterable {
    yield 1;
    yield 2;
    yield 3;
}
// Loop over `numbers()`, binding each item to `$n`
foreach (numbers() as $n) {
    // Print `$n . "\n"` to the output
    echo $n . "\n";
}
?>

Generators का One-Time-Use Nature

किसी array के उलट, जिस पर आप जितनी बार चाहें loop चला सकते हैं, एक Generator सिर्फ एक बार iterate किया जा सकता है -- एक बार किसी generator पर एक foreach loop खत्म हो जाए, उसी generator object पर फिर loop चलाना कुछ भी produce नहीं करता, क्योंकि इसकी internal position पहले से अंत तक advance हो चुकी है।

उदाहरण: The One-Time-Use Nature of Generators

php
<?php
function gen() {
    yield 1;
    yield 2;
}
$g = gen();
foreach ($g as $v) { echo $v . "\n"; }
foreach ($g as $v) { echo "second pass: $v\n"; } // never runs
echo "done";
?>

iterable बनाम array: सही Type Hint चुनना

array specifically तब इस्तेमाल करें जब किसी function को genuinely count(), sorting, या key से random access जैसी array-only capabilities चाहिए हों।

iterable तब इस्तेमाल करें जब किसी function को सिर्फ foreach से एक बार values में चलना हो -- बिना किसी real cost के inputs की एक wider range accept करते हुए।

उदाहरण: iterable vs array: Choosing the Right Type Hint

php
<?php
function countItems(array $items) {
    return count($items); // needs real array capabilities
}
function printItems(iterable $items) {
    foreach ($items as $item) {
        echo $item . "\n"; // only ever loops
    }
}
echo countItems([1, 2, 3]);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि iterable specifically "array" का मतलब है, और फिर iterable से type-hinted एक parameter पर array-only functions (जैसे array_push()) call करना, जो fail हो जाता है अगर इसके बजाय एक Traversable object pass किया जाए।
  2. यह भूल जाना कि एक iterable parameter एक Generator हो सकता है, जिस पर सिर्फ एक बार loop चलाया जा सकता है -- इसे दूसरी बार loop चलाना कुछ नहीं देता।
  3. किसी parameter को array type-hint करना जब iterable ज़्यादा flexible होगा और उतना ही safe उस function के लिए जो सिर्फ अपने input पर foreach से loop चलाता है।
चैप्टर सारांश
  • iterable एक pseudo-type है (PHP 7.1 से available) जो किसी भी ऐसी चीज़ को represent करता है जिस पर foreach से loop चलाया जा सके: एक array, या Traversable implement करने वाला कोई भी object।
  • iterable की तरह hinted एक function parameter या return type plain arrays और iterator/generator objects दोनों को interchangeably accept करता है।
  • क्योंकि एक Generator सिर्फ एक बार iterate किया जा सकता है, एक iterable receive करने वाले code को बिना check किए यह नहीं मानना चाहिए कि इस पर कई बार loop चलाया जा सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.