PHP Iterator Interface
In this page:
class ClassName implements Iterator {
function current() { } // value at the current position
function key() { }
function next() { }
function rewind() { }
function valid() { } // is the position valid?
}
foreach ($object as $key => $value) { }
Iterator Interface क्या है?
PHP का built-in Iterator interface किसी custom object को यह define करने देता है कि यह foreach loop के अंदर exactly कैसे behave करे, पाँच specific methods implement करके।
इसके बिना, foreach सिर्फ plain arrays और public properties वाले objects पर काम करता है -- Iterator आपको किसी भी internal data structure पर iteration control करने देता है, उन्हें सहित जो एक simple array की तरह stored नहीं हैं।
उदाहरण: What Is the Iterator Interface?
<?php
// Define the class `Numbers`, implementing `Iterator`
class Numbers implements Iterator {
// Declare the private property `$items`, set to `[10, 20, 30]`
private $items = [10, 20, 30];
// Declare the private property `$pos`, set to `0`
private $pos = 0;
// Define the function `current` with no parameters
function current() { return $this->items[$this->pos]; }
// Define the function `key` with no parameters
function key() { return $this->pos; }
// Define the function `next` with no parameters
function next() { $this->pos++; }
// Define the function `rewind` with no parameters
function rewind() { $this->pos = 0; }
// Define the function `valid` with no parameters
function valid() { return isset($this->items[$this->pos]); }
}
// Loop over `new Numbers()`, binding each item to `$n`
foreach (new Numbers() as $n) {
// Print `$n . "\n"` to the output
echo $n . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
पाँच Required Methods
current() current position पर value return करता है, key() इसकी key return करता है, next() internal pointer आगे बढ़ाता है, rewind() इसे शुरुआत में reset करता है (पहली iteration से पहले एक बार call होता है), और valid() एक boolean return करता है जो foreach को बताता है कि loop जारी रखना है या नहीं।
PHP हर बार जब आपका object एक foreach में इस्तेमाल होता है इन्हें automatically एक specific sequence में call करता है।
उदाहरण: The Five Required Methods
<?php
// Define the class `Numbers`, implementing `Iterator`
class Numbers implements Iterator {
// Declare the private property `$items`, set to `[10, 20]`
private $items = [10, 20];
// Declare the private property `$pos`, set to `0`
private $pos = 0;
// Define the function `current` with no parameters
function current() { return $this->items[$this->pos]; }
// Define the function `key` with no parameters
function key() { return $this->pos; }
// Define the function `next` with no parameters
function next() { $this->pos++; }
// Define the function `rewind` with no parameters
function rewind() { $this->pos = 0; }
// Define the function `valid` with no parameters
function valid() { return isset($this->items[$this->pos]); }
}
// Create a new `Numbers` instance, stored in `$it`
$it = new Numbers();
$it->rewind();
// Keep looping while `$it->valid()` holds
while ($it->valid()) {
echo $it->key() . ": " . $it->current() . "\n";
$it->next();
}
?>
Login to try C/C++/Java/PHP code in the editor
एक Custom Iterator Implement करना
एक typical implementation एक internal array और एक integer position pointer wrap करती है, हर interface method को उन दो properties पर simple operations को delegate करते हुए।
यह pattern आपको current() या valid() के अंदर validation, lazy computation, या filtering logic add करने देता है जो एक plain array कभी नहीं कर सकता।
उदाहरण: Implementing a Custom Iterator
<?php
// Define the class `EvenNumbers`, implementing `Iterator`
class EvenNumbers implements Iterator {
// Declare the private property `$items`
private $items;
// Declare the private property `$pos`, set to `0`
private $pos = 0;
// Define the constructor `__construct` taking `$items`
function __construct($items) { $this->items = $items; }
// Define the function `current` with no parameters
function current() { return $this->items[$this->pos]; }
// Define the function `key` with no parameters
function key() { return $this->pos; }
// Define the function `next` with no parameters
function next() { $this->pos++; }
// Define the function `rewind` with no parameters
function rewind() { $this->pos = 0; }
// Define the function `valid` with no parameters
function valid() {
// Return `isset($this->items[$this->pos]) && $this->items[$this->pos] % 2 === 0` from this function
return isset($this->items[$this->pos]) && $this->items[$this->pos] % 2 === 0;
}
}
foreach (new EvenNumbers([2, 4, 6]) as $n) {
echo $n . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
एक Shortcut की तरह IteratorAggregate
हाथ से सभी पाँच Iterator methods implement करना verbose है जब आपकी class बस एक मौजूदा array या object wrap करती है।
IteratorAggregate को सिर्फ एक method चाहिए, getIterator(), जो एक Iterator (अक्सर आपके internal data को wrap करने वाला एक ArrayIterator) return करता है -- PHP actual iteration को delegate कर देता है जो भी आप return करें।
उदाहरण: IteratorAggregate as a Shortcut
<?php
// Define the class `Collection`, implementing `IteratorAggregate`
class Collection implements IteratorAggregate {
// Declare the private property `$items`, set to `["a", "b", "c"]`
private $items = ["a", "b", "c"];
// Define the function `getIterator` with no parameters
function getIterator(): Iterator {
// Return `new ArrayIterator($this->items)` from this function
return new ArrayIterator($this->items);
}
}
// Loop over `new Collection()`, binding each item to `$item`
foreach (new Collection() as $item) {
// Print `$item . "\n"` to the output
echo $item . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
एक Custom Iterator कब इस्तेमाल करें
Custom iterators तब मायने रखते हैं जब underlying data एक plain array न हो -- उदाहरण के लिए, database result rows को एक-एक करके iterate करना बिना उन सबको memory में load किए, या एक defined traversal order में एक tree/linked-list structure में चलना।
एक class के लिए जो बस किसी array का एक thin wrapper है, ArrayIterator के साथ IteratorAggregate लगभग हमेशा simpler है।
उदाहरण: When to Use a Custom Iterator
<?php
class SimpleList implements IteratorAggregate {
private $items = [1, 2, 3];
function getIterator(): Iterator {
return new ArrayIterator($this->items);
}
}
// A thin array wrapper is simpler with IteratorAggregate than a full Iterator
foreach (new SimpleList() as $item) {
echo $item . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
- पाँचों methods
current,key,next,rewindऔरvalidimplement न करना, जो एक fatal error का कारण बनता है। rewindमें position reset करना भूल जाना, ताकि एक दूसरे loop को कुछ न मिले।validसे गलत value return करना, ताकि loop कभी खत्म ही न हो या तुरंत खत्म हो जाए।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: