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
 
Start new topic
Odpowiedzi (1 - 17)
mike
post
Post #2





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

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


1. Przyjęta kolejnośc to tylko i wyłącznie decyzja autorów. Ja też uważam że powinno być Instrument, Musician, Band
2. Taki zapis oznacza wymuszenie typu. public function addInstrument (Instrument $instrument); oznacza że parametr $instrument musi byc instancją klasy Instrument. Więcej: Podręcznik php :: Classes and Objects (php 5) :: Type Hinting
Go to the top of the page
+Quote Post
bela
post
Post #3


Administrator PHPedia.pl


Grupa: Developerzy
Postów: 1 102
Pomógł: 2
Dołączył: 14.09.2003

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


Cytat
2. Taki zapis oznacza wymuszenie typu. public function addInstrument (Instrument $instrument); oznacza że parametr $instrument musi byc instancją klasy Instrument.

Albo, co chyba najważniejsze, jej potomnych.
Go to the top of the page
+Quote Post
Jarod
post
Post #4





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

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


Cytat(mike_mech @ 6.08.2006, 00:16 ) *
2. Taki zapis oznacza wymuszenie typu. public function addInstrument (Instrument $instrument); oznacza że parametr $instrument musi byc instancją klasy Instrument.


Musi być instancją klasy intrument czy klas, które implementują interfejs instrument?
Go to the top of the page
+Quote Post
mike
post
Post #5





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

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


Cytat(J4r0d @ 6.08.2006, 11:08 ) *
Musi być instancją klasy intrument czy klas, które implementują interfejs instrument?

Hmm, nieprecyzyjnie napisałem.
Musi być instancją klas implementujących interfejs Instrument, lub jak ~bela zauważył klas pochodnych do tych klas.


P.S.
Nie można utworzyć instancji interfejsu.
Go to the top of the page
+Quote Post
Jarod
post
Post #6





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

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


Cytat(mike_mech @ 6.08.2006, 09:13 ) *
Hmm, nieprecyzyjnie napisałem.
Musi być instancją klas implementujących interfejs Instrument, lub jak ~bela zauważył klas pochodnych do tych klas.
P.S.
Nie można utworzyć instancji interfejsu.


Czyli do metody public function addInstrument (Instrument $instrument); można przekazać wszystkie obiekty, które implementują interfejs Instrument?

  1. <?php
  2. class Klasa1 implements Instruments {
  3.  
  4. public function addInstrument(Instrument $instrument) {
  5. array_push($this->instruments, $instrument)
  6. } 
  7. ...
  8. }
  9.  
  10. class Klasa2 implements Instruments {
  11.  
  12. public function addInstrument(Instrument $instrument) {
  13. array_push($this->instruments, $instrument)
  14. } 
  15. ...
  16. }
  17.  
  18. class Klasa3 implements Musician {
  19. ...
  20. ...
  21. }
  22.  
  23.  
  24. $myClass1 = new Klasa1;
  25. $myClass2 = new Klasa2;
  26. $myClass3 = new Klasa3;
  27.  
  28. $myClass1->addInstrument(Instrument $instrument); //Dobrze?
  29. $myClass2->addInstrument(Instrument $instrument); //Dobrze?
  30. $myClass3->addInstrument(Instrument $instrument); // Źle?
  31. ?>
Go to the top of the page
+Quote Post
mariuszn3
post
Post #7





Grupa: Zarejestrowani
Postów: 352
Pomógł: 0
Dołączył: 22.01.2006

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


Wymuszenie typów możesz zastosować tylko w definicji metod, kiedy wywołujesz metode nie podpowiadasz już jakiego typu jest przesłana przez Ciebie zmienna a więc tak:
  1. <?php
  2. $myClass1->addInstrument($instrument);
  3. ?>

Zakładam, że gdzieś wcześniej określiłeś zmienną $instrument i jest to instancja klasy implementującej Instrument.

Ten post edytował mariuszn3 6.08.2006, 13:34:04
Go to the top of the page
+Quote Post
Jarod
post
Post #8





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

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


  1. <?php
  2. $myClass1->addInstrument($instrument); //Dobrze?
  3. $myClass2->addInstrument($instrument); //Dobrze?
  4. $myClass3->addInstrument($instrument); // Źle?
  5. ?>


Czy to jest poprawne rozumowanie?
Go to the top of the page
+Quote Post
mariuszn3
post
Post #9





Grupa: Zarejestrowani
Postów: 352
Pomógł: 0
Dołączył: 22.01.2006

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


Nie wiem rozumowanie jakiej kwestii masz na myśli.. ale zapis jest jak najbardziej prawidłowy..
Go to the top of the page
+Quote Post
Jarod
post
Post #10





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

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


Cytat(mariuszn3 @ 6.08.2006, 12:43 ) *
Nie wiem rozumowanie jakiej kwestii masz na myśli.. ale zapis jest jak najbardziej prawidłowy..


Chodzi mi o to czy dobrze rozumiem, że
  1. <?php
  2. $myClass3->addInstrument($instrument);
  3. ?>
jest błędne
Go to the top of the page
+Quote Post
mariuszn3
post
Post #11





Grupa: Zarejestrowani
Postów: 352
Pomógł: 0
Dołączył: 22.01.2006

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


Jest błędne tylko dlatego, że Klasa3 nie ma zdefiniowanej metody addInstrument()
Go to the top of the page
+Quote Post
Jarod
post
Post #12





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

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


Cytat(mariuszn3 @ 6.08.2006, 13:06 ) *
Jest błędne tylko dlatego, że Klasa3 nie ma zdefiniowanej metody addInstrument()


A nawet gdyby miała to i tak byłoby błędnie. Przecież nie implementuje interfejsu Instrument.. :/
Go to the top of the page
+Quote Post
mariuszn3
post
Post #13





Grupa: Zarejestrowani
Postów: 352
Pomógł: 0
Dołączył: 22.01.2006

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


To, że nie implementuje nie oznacza, że jej metody nie mogą przyjmować instancji obiektów które implementują instrument.
Go to the top of the page
+Quote Post
Jarod
post
Post #14





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

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


Cytat(mariuszn3 @ 6.08.2006, 13:19 ) *
To, że nie implementuje nie oznacza, że jej metody nie mogą przyjmować instancji obiektów które implementują instrument.



Zgłupiałem już:
Cytat(mike_mech @ 6.08.2006, 09:13 ) *
Hmm, nieprecyzyjnie napisałem.
Musi być instancją klas implementujących interfejs Instrument, lub jak ~bela zauważył klas pochodnych do tych klas.
Go to the top of the page
+Quote Post
mariuszn3
post
Post #15





Grupa: Zarejestrowani
Postów: 352
Pomógł: 0
Dołączył: 22.01.2006

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


No dobrze.. ale co musi być instancją tych klas? Klasa do której metody się odwołujesz.. czy obiekt, który przekazujesz do metody? (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) ..przyjrzyj się temu uważnie.
Go to the top of the page
+Quote Post
Jarod
post
Post #16





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

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


Cytat(mariuszn3 @ 6.08.2006, 13:30 ) *
No dobrze.. ale co musi być instancją tych klas? Klasa do której metody się odwołujesz.. czy obiekt, który przekazujesz do metody? (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) ..przyjrzyj się temu uważnie.


Nie wiem już o czym piszesz. Możesz to jakoś jaśniej wytłumaczyć?
Go to the top of the page
+Quote Post
mariuszn3
post
Post #17





Grupa: Zarejestrowani
Postów: 352
Pomógł: 0
Dołączył: 22.01.2006

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


Przyjrzyj się uważnie podanemu przez siebie przykładowi z książki i temu co Ty później napisałeś. Kluczową różnicą jest to, że w przykładzie z książki klasa Guitarist nie implementuje interfejsu Instrument.. nie mniej jej metoda addInstrument() przyjmuje obiekty, które implementują instrument..
To co później napisałeś to coś zupełnie innego - stwierdzenie, że jeśli klasa sama w sobie nie implementuje danego interfejsu to jej metody nie mogą przyjmować obiektów, które implementują dany interfejs co jest z zasady bzdurnym założeniem.
Go to the top of the page
+Quote Post
envp
post
Post #18





Grupa: Zarejestrowani
Postów: 359
Pomógł: 1
Dołączył: 16.04.2006
Skąd: Łódź

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


Stary, nie zagłębiaj się, aż tak, bo po pierwsze w tej książce w kodach są błędy, po drugie, autorzy napisali ją tak jakby to było dla nich karą a po trzecie do OOP trzeba dorosnąć i i tego nei nauczysz się tak o .. pstryk i juz (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) OOP to idea programowania. Przeczytaj gdzies tak do narzedzi uzytecznych czy jak to tam bylo i od tamtad zacznij dopiero psiac (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) . Pozatym uzupelniaj sie googlem, bo naprawde ta ksiazka jest 'zakrecowna'
Go to the top of the page
+Quote Post

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: 23.08.2025 - 21:21