मैं, this one की तरह एक इटरेटर बनाने के लिए कोशिश कर रहा हूँ टिप्पणियों की सूची के लिए में बहु स्तरीय "इटरेटर" को लागू करने:पीएचपी
// the iterator class, pretty much the same as the one from the php docs...
abstract class MyIterator implements Iterator{
public $position = 0,
$list;
public function __construct($list) {
$this->list = $list;
$this->position = 0;
}
public function rewind() {
$this->position = 0;
}
public function current() {
return $this->list[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
++$this->position;
}
public function valid() {
return isset($this->list[$this->position]);
}
}
टिप्पणी इटरेटर:
class MyCommentIterator extends MyIterator{
public function current(){
return new Comment($this->list[$this->position]);
}
}
और यह है मैं इसका उपयोग कैसे करता हूं:
$comments = GetComments(); // gets the comments from the db
if($comments): ?>
<ol>
<?php foreach(new MyCommentIterator($comments) as $comment): ?>
<li>
<p class="author"><?php echo $comment->author(); ?></p>
<div class="content">
<?php echo $comment->content(); ?>
</div>
<!-- check for child comments and display them -->
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
तो सब कुछ ठीक काम कर रहा है, एक चीज़ के अलावा: मैं यह समझ नहीं सकता कि एन को कैसे संसाधित किया जाए दिलचस्पी टिप्पणी :(
$comments
सरणी की तरह टिप्पणियों का एक फ्लैट सूची देता है,:
[0] => object(
'id' => 346,
'parent' => 0, // top level comment
'author' => 'John',
'content' => 'bla bla'
),
[1] => object(
'id' => 478,
'parent' => 346, // child comment of the comment with id =346
'author' => 'John',
'content' => 'bla bla'
)
...
मैं किसी भी तरह (कई स्तरों पर) बच्चे टिप्पणियों के लिए जाँच करने में सक्षम हो और </li>
'से पहले उन्हें डालने की आवश्यकता उनके माता-पिता की टिप्पणियों के ...
कोई विचार?