Witam.Rozumiem obiektówke ale nie potrafie zrozumień itelatora.Ogólnie niektóre czynności z nim związane.Mam przykład:
<?php
class nasz_iterator implements Iterator{
private $i;
private $obj;
public function __construct($obiekt){
$this -> obj = $obiekt;
$this -> i = 0;
} // end __construct();
$this -> i = 0;
} // end rewind();
public function valid(){
} // end valid();
return $this -> i;
} // end key();
return $this -> obj -> get_item($this -> i);
} // end current();
$this -> i++;
} // end next();
}
class x_file implements IteratorAggregate{
protected $plik;
public function __construct($plik){
$this -> plik = file($plik); }else{
$this -> plik = array(0 => ''); }
} // end __construct();
public function getIterator(){
return new nasz_iterator($this);
} // end getIterator();
public function get_item($i){
return trim($this -> plik[$i]); } // end get_item();
return isset($this -> plik[$i]); } // end key_exists();
}
class x_list extends x_file{
public function __construct(){
} // end __construct();
public function add($item){
$this -> plik[] = $item;
return count($this -> plik) - 1; } // end add();
}
$plix = new x_file('words.txt');
foreach($plix as $lnum => $linia){
echo $lnum.' - '.$linia.'<br/>'; }
$lista = new x_list;
$lista -> add('pozycja 1');
$lista -> add('pozycja 2');
$lista -> add('pozycja 3');
foreach($lista as $id => $pozycja){
echo $id.' - '.$pozycja.'<br/>'; }
?>
Mam tu przykład.I nie rozumiem dlaczego po uruchomieniu klasy x_file .Pierw uruchamia się metoda getIterator,potem key_exists a potem get_item.Dlaczego akurat w tej kolejności? Proszę o dokładne wytłumaczenie.Ponieważ w manualu nie pisze dokładnie dlaczego tak się to uruchamia.Nie rozumiem poprostu tego,
dlaczego w takiej kolejności się uruchamiają funkcje a nie w innej
Ten post edytował szypi1989 24.12.2010, 12:49:30