Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: dziedziczenie
Forum PHP.pl > Forum > PHP > Object-oriented programming
Apo
Witam
Tak dla ćwiczenia napisałem sobie 3 klasy. 1 glówna i 2 pochodne.
Oto kod:

  1. <?php
  2. class ShopProducts {
  3. public $thing;
  4. public $autorname;
  5. public $autorlastname;
  6. public $how;
  7.  
  8. function _construct($thing, $autorname, $autorlastname, $how)
  9. {
  10. $this->thing = $thing;
  11. $this->autorname = $autorname;
  12. $this->autorlastname= $autorlastname;
  13. $this->how = $how;
  14. }
  15. }
  16.  
  17. class CdProduct extends ShopProducts {
  18. public $howlong;
  19.  
  20. function __construct($thing, $autorname, $autorlastname, $how, $howlong)
  21. {
  22. parent::__construct($thing, $autorname, $autorlastname, $howlong);
  23. $this->howlong = $howlong;
  24. }
  25.  
  26. function opis()
  27. {
  28. $desc = 'Dane autora: '.$this->autorname.' '.$this->autorlastname.'<br />
  29. Towar: '.$this->thing.' w ilości: '.$this->how.'<br />
  30. Czas nagrania: '.$this->howlong;
  31. return $desc;
  32. }
  33. }
  34.  
  35. class BookProduct extends ShopProducts {
  36. public $howpages;
  37.  
  38. function __construct($thing, $autorname, $autorlastname, $how, $howpages)
  39. {
  40. parent::__construct($thing, $autorname, $autorlastname, $how);
  41. $this->howpages = $howpages;
  42. }
  43.  
  44. function opis()
  45. {
  46. $desc = 'Dane autora: '.$this->autorname.' '.$this->autorlastname.'<br />
  47. Towar: '.$this->thing.' w ilości: '.$this->how.'<br />
  48. Ilosć stron: '.$this->howpages;
  49. return $desc;
  50. }
  51. }
  52.  
  53. $pirat = new CdProduct('Bloodhiund Gang', 'Marek', 'Kowalski', 5, '15min');
  54. echo $pirat->opis;
  55. ?>


No i według mnie wszystko dobrze napisałem ale wyskakuje mi błąd:
Fatal error: Can not call constructor in index.php on line 22
a linia 22 to:
  1. <?php
  2. parent::__construct($thing, $autorname, $autorlastname, $how);
  3. ?>

sad.gif
ikioloak
  1. <?php
  2.  
  3. class ShopProducts {
  4. public $thing;
  5. public $autorname;
  6. public $autorlastname;
  7. public $how;
  8.  
  9.     function __construct($rzecz, $imie, $nazwisko, $ile) //zjadles jedna dolna spacje :)
  10.     {
  11.     $this->thing        = $rzecz;
  12.     $this->autorname    = $imie;
  13.     $this->autorlastname= $nazwisko;
  14.     $this->how            = $ile;
  15.     }
  16. }
  17.  
  18. ?>

Przy metodach tez powinienes definiowac czy jest prywatna/publiczna/chroniona
mike
Przecież w klasie ShopProducts nie ma zdefiniowanej metody __construct() tylko jest _construct() tongue.gif
Więc php nie może jej wywołać, bo jej nie ma biggrin.gif

---edit---
~ikioloak wyprzedziłes mnie winksmiley.jpg
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.