Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dziedziczenie obiektów, a raczej tablicy z obiektami
SHiP
post
Post #1





Grupa: Zarejestrowani
Postów: 697
Pomógł: 47
Dołączył: 19.12.2003
Skąd: Lublin

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


Mam mały problem z dziedziczeniem wartosci ;]

  1. <?php
  2. class other
  3. {
  4. function check()
  5. {
  6. echo 'ok<br/> ';
  7. }
  8. }
  9.  
  10. class ext extends main
  11. {
  12. public function __construct()
  13. {
  14. echo $this -> example.'<br/>'; //ok
  15. print_r($this -> myObjects); // puste?
  16. $this -> myObjects['other'] -> check(); // nie dziala :/
  17. }
  18.  
  19. public function ble()
  20. {
  21. }
  22. }
  23.  
  24. class main
  25. {
  26. public $myObjects=Array();
  27. public $example='Example text';
  28.  
  29. public function __construct()
  30. {
  31. $this -> myObjects['other'] = new other();
  32. $this -> myObjects['other'] -> check(); // ok
  33. $this -> myObjects['ext'] = new ext();
  34. }
  35. }
  36.  
  37. $mainObj = new main();
  38. ?>


Jeżeli stworzony przez obiekt klasy main obiekt klasy other zapisze w tablicy publicznej to obiekt klasy dziedziczącej tą tablice nie widzi jej elementow ;] Dlaczego?

Szukam powiązania tak aby po stworzeniu obiektów 2 klas jeden uzyskał dostęp do drugiego po przez pośrednika - klase main ;]. Próbowałem stworzyć funkcję getIterate w klasie main ;] ale chyba coś pomieszałem bo nie działało... jakieś pomysły?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
SHiP
post
Post #2





Grupa: Zarejestrowani
Postów: 697
Pomógł: 47
Dołączył: 19.12.2003
Skąd: Lublin

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


Dzięki ale nadal mam problem... w moim przypadku konstruktor tworzy 2 obiekty(w tym obiekt klasy ext) wiec jego wywołanie spod klasy ext spowoduje zapętlenie programu... Nawet jeśli wyklucze zapętlenie dodając zmienną z nazwą klasy pochodnej to pozostaje problem dostępu do pozostałych klas... w końcu nie moge za każdym razem tworzyć nowych obiektów ;]

Ok posiedziałem troche, przewertowałem przykłady z manuala i wymyśliłem (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)
  1. <?php
  2.  
  3. class other
  4. {
  5. function check()
  6. {
  7. echo 'ok<br/> ';
  8. }
  9. }
  10.  
  11. class main
  12. {
  13. static $myObjects=Array();
  14. public $example='Example text';
  15.  
  16. public function __construct()
  17. {
  18. $this -> myObjects['other'] = new other();
  19. }
  20.  
  21. public function run()
  22. {
  23. echo "bezposrednio: ";
  24. print_r($this -> myObjects);
  25. echo "<br/><Br/>";
  26. $this -> myObjects['ext'] = new ext($this);
  27. }
  28. }
  29.  
  30. class ext
  31. {
  32. var $parentNode;
  33.  
  34. public function __construct(&$parent)
  35. {
  36.  $this -> parentNode = & $parent;
  37. echo "wewnatrz<br/>=============<br/>";
  38.  print_r($this -> parentNode);
  39.  
  40.  print_r($this -> parentNode ->myObjects);
  41. echo "==============<br/>";
  42. // print_r($this -> myObjects); // puste?
  43. $this -> parentNode -> myObjects['other'] -> check(); // dziala :)
  44. }
  45.  
  46. }
  47.  
  48.  
  49. echo "<pre>";
  50. $mainObj = new main();
  51. $mainObj -> run();
  52. $mainObj -> myObjects['other'] -> check(); // ok
  53. ?>


Ten post edytował SHiP 11.07.2006, 21:14:04
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 13.10.2025 - 11:34