I am given the following code:
Code: Select all
<?php
$test = new testClass();
$test->a = 'a';
$test->b = 'b';
foreach( $test as $key => $value ){
echo $key . ' : ' . $value . "\n";
}
?>
2) I need to finish the above code, so it displays
a : a
b : b
3) If new elements are added to testClass, they should be displayed as well.
I undesrtand the idea of Iterator, i can write it for my own custom classe (for example, to iterate via collection in my class or database data). However, I cannot understand the idea of this particular excercise - what custom things should be implemented in my iterator? How my custom Iterator should iterate?
Actually, I can simply create class testClass with two public variables "public a" and "public b" and that's it, why I need iterator??? Foreach loop already iterates through all public variables by default...????
Could you please help me to figure out the idea of the excercise???
Renate