$this->students[] = $student; } } public function contains(Student $student) { foreach ($this->students as $s) { if ($s->getId() == $student->getId()) { return true; } } return false; } public function getIterator() { return new CachingIterator($this->students->getIterator(), true); } } $students = new StudentList(); $students->add(new Student('01234123', 'Joe')); $students->add(new Student('00000014', 'Bob')); $students->add(new Student('00000014', 'Foo')); // The goal is to verify we can access the cached string value even if it was // generated by a call to __toString(). To check this we need to access the // iterator's __toString() method. $it = $students->getIterator(); foreach ($it as $student) { echo $it->__toString(), "\n"; } ?> ===DONE===
} public function add(Student $student) { if (!$this->contains($student)) { $this->students[] = $student; } } public function contains(Student $student) { foreach ($this->students as $s) { if ($s->getId() == $student->getId()) { return true; } } return false; } public function getIterator() { return $this->students->getIterator(); } } $students = new StudentList(); $students->add(new Student('01234123', 'Joe')); $students->add(new Student('00000014', 'Bob')); $students->add(new Student('00000014', 'Foo')); foreach ($students as $student) { echo $student, "\n"; } ?> ===DONE===
<?php require 'Student.php'; require 'StudentList.php'; require 'StudentListIterator.php'; $student1 = new Student('Josay', 'Posay'); $student2 = new Student('Marty', 'McFly'); $student3 = new Student('Manuel', 'Valls'); $list = new StudentList(); $iterator = new StudentListIterator($list); $list->addStudent($student1)->addStudent($student2)->addStudent($student3); while ($iterator->hasNext()) { echo 'ELEMENT COURANT : <br>'; echo '<pre>'; var_dump($iterator->getCurrent()); echo '</pre>'; echo 'ELEMENT SUIVANT: <br>'; echo '<pre>'; var_dump($iterator->getNext()); echo '</pre>'; }