PHP – Make Iterator with PHP Class

PHP and iterators…

Violin and boxing…

Anyway, it is possible and the possibility is since some years ago.

In this article I will simply show how to make an iterator with a PHP class. And I would iterate in a table, with different background colors per row. And that is it!

iterator

In order to make an iterator with Php class, we need to define 5 functions in the class:

  1. current()
  2. key()
  3. next()
  4. rewind()
  5. valid()

Once this is done, the iterator is ready :). And we may access it with the help of a foreach array.

Here comes the code:

<?
    class TopGames implements Iterator{
        
        public $price;
        public $game_collection = array();
    
        private $counter;
        
        public function current(){
            return $this->game_collection[$this->counter];    
        }
        
        public function key(){
            return $this->counter;
        }
        
        public function next(){
            $this->counter++;
        }
        
        public function rewind(){
            $this->counter = 1;
        }
        
        public function valid(){
            return isset($this->game_collection[$this->counter]);
        }        
    }
    
    $my_games = new TopGames;
    $my_games->price = 100;
    $my_games->game_collection = array("WC3 TFT", "AOE2", "Collin McRea Rally 2","Soldier of Fortune","Fifa 2001","Zanzarah - the hidden portal Demo");
    $my_str = "<table border = 7  >";
    $my_str .= "<tr><td>#</td><td>Price</td><td>Name</td></tr>";
    foreach ($my_games as $pc_game => $interesting_game){
        $my_str .= "<tr><td bgcolor = #ABCCAA>$pc_game </font></td><td bgcolor = #FAFACC>".round_up($my_games->price,3)." &euro;</td><td bgcolor = #AACACE>$interesting_game</td></tr>";
        $my_games->price *= 1.35;
        
    }
    function round_up($number, $precision = 2){
        $fig = (int) str_pad('1', $precision, '0');
        return (ceil($number * $fig) / $fig);
    }
    
    $my_str .="</table>";
    echo $my_str;
?>

Use responsibly. In Github.

🙂