for example in this code i have here:
<?php
include_once 'StudentClass.php'; // Our business logic
class MyIterator implements Iterator
{
private $var = array();
public function __construct($array)
{
if (is_array($array)) {
$this->var = $array;
}
}
public function rewind() {
echo "rewinding\n";
reset($this->var);
}
public function current() {
$var = current($this->var);
echo "current: $var\n";
return $var;
}
public function key() {
$var = key($this->var);
echo "key: $var\n";
return $var;
}
public function next() {
$var = next($this->var);
echo "next: $var\n";
return $var;
}
public function valid() {
$var = $this->current() !== false;
echo "valid: {$var}\n";
return $var;
}
}
$student = &new StudentClass;
$student->setUserName("TestName");
$values = array($student);
$it = new MyIterator($values);
foreach ($it as $a => $b) {
print "$a->getUserName() === $b\n";
}
?>
i have added the student to an array. How do i get their name inside the foreach loop?
and how do i append an array so i can add more students?
for ($i=0;$i<10;$i++)
{
$student = &new StudentClass;
$student->setUserName("TestName");
$values = array();
$values[] = $student->getUserName();
$it = new MyIterator($values);
}
foreach ($it as $a => $b) {
print "$a === $b\n";
}