लागू करता है। मैं अपने सिर को एसपीएल इटरेटर्स के आसपास लाने की कोशिश कर रहा हूं और मैं इसे संभालने के 2 तरीके लेकर आया हूं। मैं पहला संस्करण कम जटिल होने के लिए देखता हूं लेकिन दूसरे संस्करण में रचना को महसूस होता है (मुझे लगता है)।क्या कोई ऑब्जेक्ट एक इटरेटर को कार्यान्वित करता है या किसी अन्य ऑब्जेक्ट को लागू करता है जो एक इटरेटर
मैं क्या नहीं देख रहा हूं कि दूसरे पर कौन सा बेहतर है? या मैं बस इसे जटिल बना रहा हूँ?
वस्तु पुनरावर्तक लागू करता है::
class BoxOfChocolates implements Iterator {
private $id
private $name; // e.g. Valentine's Heart Box
private $maker; // e.g. Hersheys
private $items; // array
public function getChocolates() {
$query = ...
foreach($rows as $row) {
$this->_items[] = new Chocolate() ...
}
}
// ... necessary iterator implementation stuff
}
वस्तु एक पुनरावृति करने योग्य वस्तु शामिल हैं:
class BoxOfChocolates {
private $id;
private $name;
private $maker;
private $items; // chocolates object
public function getChocolates() {
$this->items = new Chocolates();
$this->items->getChocolates();
}
}
class Chocolates implements Iterator {
private $items;
public function getChocolates() {
$query = ...
foreach($rows as $row) {
$this->_items[] = new Chocolate() ...
}
}
// ... necessary iterator implementation stuff
}