Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Na czym polega działanie itelatora?, Na czym polega działanie itelatora?
szypi1989
post 24.12.2010, 12:47:55
Post #1





Grupa: Zarejestrowani
Postów: 207
Pomógł: 0
Dołączył: 7.09.2010

Ostrzeżenie: (0%)
-----


Witam.Rozumiem obiektówke ale nie potrafie zrozumień itelatora.Ogólnie niektóre czynności z nim związane.Mam przykład:
  1. <?php
  2.  
  3. class nasz_iterator implements Iterator{
  4. private $i;
  5. private $obj;
  6.  
  7. public function __construct($obiekt){
  8. $this -> obj = $obiekt;
  9. $this -> i = 0;
  10. } // end __construct();
  11.  
  12. public function rewind(){
  13. $this -> i = 0;
  14. } // end rewind();
  15.  
  16. public function valid(){
  17. return $this -> obj -> key_exists($this -> i);
  18. } // end valid();
  19.  
  20. public function key(){
  21. return $this -> i;
  22. } // end key();
  23.  
  24. public function current(){
  25. return $this -> obj -> get_item($this -> i);
  26. } // end current();
  27.  
  28. public function next(){
  29. $this -> i++;
  30. } // end next();
  31. }
  32.  
  33.  
  34.  
  35.  
  36. class x_file implements IteratorAggregate{
  37. protected $plik;
  38.  
  39. public function __construct($plik){
  40. if(file_exists($plik)){
  41. $this -> plik = file($plik);
  42. }else{
  43. $this -> plik = array(0 => '');
  44. }
  45. } // end __construct();
  46.  
  47. public function getIterator(){
  48. return new nasz_iterator($this);
  49. } // end getIterator();
  50.  
  51. public function get_item($i){
  52. return trim($this -> plik[$i]);
  53. } // end get_item();
  54.  
  55. public function key_exists($i){
  56. return isset($this -> plik[$i]);
  57. } // end key_exists();
  58. }
  59.  
  60.  
  61.  
  62. class x_list extends x_file{
  63. public function __construct(){
  64. $this -> plik = array();
  65. } // end __construct();
  66.  
  67. public function add($item){
  68. $this -> plik[] = $item;
  69. return count($this -> plik) - 1;
  70. } // end add();
  71. }
  72.  
  73. $plix = new x_file('words.txt');
  74. foreach($plix as $lnum => $linia){
  75. echo $lnum.' - '.$linia.'<br/>';
  76. }
  77.  
  78. $lista = new x_list;
  79. $lista -> add('pozycja 1');
  80. $lista -> add('pozycja 2');
  81. $lista -> add('pozycja 3');
  82.  
  83. echo '<br/>';
  84.  
  85. foreach($lista as $id => $pozycja){
  86. echo $id.' - '.$pozycja.'<br/>';
  87. }
  88. ?>


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
Go to the top of the page
+Quote Post
Mephistofeles
post 24.12.2010, 15:18:27
Post #2





Grupa: Zarejestrowani
Postów: 1 182
Pomógł: 115
Dołączył: 4.03.2009
Skąd: Myszków

Ostrzeżenie: (0%)
-----


Ponieważ tak je wywołuje silnik PHP, dlatego aby to działało wymagane jest zaimplementowanie interfejsu Iterator, żeby mieć pewność, że klasa posiada odpowiednie metody.

Btw iteRator, tak jak w kodzie.

Ten post edytował Mephistofeles 24.12.2010, 15:18:56
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 8.07.2025 - 01:51