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
NuLL
post
Post #2





Grupa: Zarejestrowani
Postów: 2 262
Pomógł: 21
Dołączył: 3.05.2004
Skąd: Sopot, Krakow, W-wa

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


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

php to nie C++ - konstruktor klasy macierzystej trzeba jawnie wywolac.
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: 5.10.2025 - 10:04