Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Implementacja interfejów + polimorfizm
Forum PHP.pl > Forum > PHP > Object-oriented programming
cojack
  1. <?php
  2. interface Band {
  3.      public function getName();
  4.      public function getGenre();
  5.      public function addMusician();
  6.      public function getMusicians();
  7.   }
  8.  
  9.   interface Musician {
  10.      public function addInstrument();
  11.      public function getInstruments();
  12.  
  13.      public function assignToBand();
  14.      public function getMusicianType();
  15.   }
  16.  
  17.   interface Instrument {
  18.      public function getName();
  19.      public function getCategory();
  20.   }
  21.  
  22.   class Guitarist implements Musician {
  23.      private $last;
  24.      private $first;
  25.      private $musicianType;
  26.  
  27.      private $instruments;
  28.      private $bandReference;
  29.  
  30.      function __construct($first, $last) {
  31.         $this->last = $last;
  32.         $this->first = $first;
  33.         $this->instruments = array();
  34.         $this->musicianType = "gitarzysta";
  35.      }
  36.  
  37.      public function getName() {
  38.         return $this->first . " " . $this->last;
  39.      }
  40.  
  41.      public function addInstrument(Instrument $instrument) {
  42.         array_push($this->instruments, $instrument);
  43.      }
  44.  
  45.      public function getInstruments() {
  46.         return $this->instruments;
  47.      }
  48.  
  49.      public function getBand() {
  50.         return $this->$bandReference;
  51.      }
  52.  
  53.      public function assignToBand(Band $band) {
  54.         $this->$bandReference = $band;
  55.      }
  56.  
  57.      public function getMusicianType() {
  58.         return $this->musicianType;
  59.      }
  60.  
  61.      public function setMusicianType($musicianType) {
  62.         $this->musicianType = $musicianType;
  63.      }
  64.  
  65.   }
  66.  
  67.   class LeadGuitarist extends Guitarist {
  68.      function __construct($last, $first) {
  69.         parent::__construct($last, $first);
  70.         $this->setMusicianType("główny gitarzysta");
  71.      }
  72.   }
  73.   class RockBand implements Band {
  74.      private $bandName;
  75.      private $bandGenre;
  76.      private $musicians;
  77.  
  78.      function __construct($bandName) {
  79.         $this->bandName = $bandName;
  80.         $this->musicians = array();
  81.         $this->bandGenre = "rock";
  82.      }
  83.  
  84.      public function getName() {
  85.         return $this->bandName;
  86.      }
  87.  
  88.      public function getGenre(){
  89.         return $this->bandGenre;
  90.      }
  91.  
  92.      public function addMusician(Musician $musician){
  93.         array_push($this->musicians, $musician);
  94.         $musician->assignToBand($this);
  95.      }
  96.  
  97.      public function getMusicians() {
  98.         return $this->musicians;
  99.      }
  100.   }
  101.  
  102.   class Guitar implements Instrument {
  103.  
  104.      private $name;
  105.      private $category;
  106.  
  107.      function __construct($name) {
  108.         $this->name = $name;
  109.         $this->category = "gitary";
  110.      }
  111.  
  112.      public function getName() {
  113.         return $this->name;
  114.      }
  115.  
  116.      public function getCategory() {
  117.         return $this->category;
  118.      }
  119.   }
  120.  
  121.   // Test obiektów
  122.   $band = new RockBand("Czerwone Zmienne");
  123.   $bandMemberA = new Guitarist("Michał", "Zmiennoprzecinkowy");
  124.   $bandMemberB = new LeadGuitarist("Grzegorz", "Całkowity");
  125.  
  126.   $bandMemberA->addInstrument(new Guitar("Gibson Les Paul"));
  127.   $bandMemberB->addInstrument(new Guitar("Fender Stratocaster"));
  128.   $bandMemberB->addInstrument(new Guitar("Hondo H-77"));
  129.  
  130.   $band->addMusician($bandMemberA);
  131.   $band->addMusician($bandMemberB);
  132.  
  133.   foreach($band->getMusicians() as $musician) {
  134.      echo "Muzyk ".$musician->getName() . "<br>";
  135.      echo "to " . $musician->getMusicianType() . "<br>";
  136.      echo "w zespole grającym " . $musician->getBand()->getGenre() . "<br>";
  137.      echo "o nazwie " . $musician->getBand()->getName() . "<br>";
  138.  
  139.      foreach($musician->getInstruments() as $instrument) {
  140.         echo "Jego instrument to " . $instrument->getName() . " ";
  141.         echo "(" . $instrument->getCategory() . ")<br>";
  142.      }
  143.      echo "<p>";
  144.   }
  145. ?>


Nie rozumiem dlaczego zwraca mi taki błąd: Fatal error: Declaration of Guitarist::addInstrument() must be compatible with that of Musician::addInstrument() in /srv/http/ksiazka/1.php on line 23.

Jeżeli nie macie nic do powiedzenia, to nie odpisujcie, nie chce żadnych komentarzy pomyśl, popatrz na 11 i 42 linijke, nie oczekuje tego. Chciałbym po prostu dowiedzieć się gdzie i co jest źle i jak to naprawić, będę bardzo wdzięczny.
-=Peter=-
Ponieważ w interfejsie Musician metoda addInstrument() nie przyjmuje żadnych argumentów.

  1. <?php
  2. interface Musician {
  3.     public function addInstrument(Instrument $instrument);//tego brakowało
  4.     public function getInstruments();
  5.  
  6.     public function assignToBand();
  7.     public function getMusicianType();
  8.  }
  9. ?>


Pozatym jeśli masz polską edycję tej książki, to pierwszym krokiem powinno być sprawdzenie erraty: http://helion.pl/errata.cgi?id=php5zp
nospor
ale czego tu nie rozumiec?
Interface Musician mowi ze ma byc taka metoda:
public function addInstrument();

a ty dajesz taką:
public function addInstrument(Instrument $instrument)

widzisz roznice?
cojack
Dzięki.
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.