Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> dziedziczenie, __construct problem
Apo
post
Post #1





Grupa: Zarejestrowani
Postów: 426
Pomógł: 1
Dołączył: 2.10.2005

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


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

Ten post edytował Apo 15.01.2006, 15:57:47
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
ikioloak
post
Post #2





Grupa: Zarejestrowani
Postów: 416
Pomógł: 0
Dołączył: 8.01.2004

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


  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

Ten post edytował ikioloak 15.01.2006, 15:58:40
Go to the top of the page
+Quote Post
mike
post
Post #3





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

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


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
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 Aktualny czas: 21.08.2025 - 21:02