Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Dziwne argumenty metod, prośba o wyjaśnienie
Jarod
post
Post #1





Grupa: Zarejestrowani
Postów: 1 190
Pomógł: 27
Dołączył: 23.04.2005

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


Przykład z książki:
  1. <?php
  2. interface Band {
  3. public function getName();
  4. public function addMusician (Musician $musician);
  5. ...
  6. }
  7.  
  8. interface Musician {
  9. public function addInstrument (Instrument $instrument);
  10. public function assignToBand(Band $band);
  11. ...
  12. }
  13.  
  14. interface Instrument {
  15. public function getName();
  16. public function getCategory();
  17. ...
  18. }
  19.  
  20.  
  21. class Guitarist implements Musician {
  22.  
  23. ...
  24. private $last;  
  25. private $instruments;  
  26. ...
  27.  
  28. public function addInstrument(Instrument $instrument) {
  29. array_push($this->instruments, $instrument)
  30. }
  31.  
  32. ...
  33. }
  34. ?>


I teraz mam prosbę o wyjaśnienie

1.
Wiadomo, że można tworzyć obiekt gdy definicja klasy występuje w kodzie po utworzeniu obiektu (nie ma konieczności napisania definicji funkcji przed jej wywołaniem). Czy nie wyraźniej (bardziej zrozumiale dla osoby, która przejmuje np. jakiś projekt) byłoby napisać te interfejsy w kolejności: Instrument, Musician, Band ?

2. W definicji interfejsów i klasy (które podałem wyżej) nie rozumię pewnej kwestii i prosiłbym osoby, które dobrze znają OOp o wytłumaczenie:

Dlaczego w interfejsie Musician jest

public function addInstrument (Instrument $instrument);
a nie
public function addInstrument ($instrument); ?

Podobnie jest w klasie Guitarist - dlaczego zamiast
public function addInstrument(Instrument $instrument)
nie jest
public function addInstrument(Instrument $instrument) ?
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: 9.10.2025 - 11:44