Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Wywolanie funkcji OOP
Forum PHP.pl > Forum > Przedszkole
Wolfie
Witam,

Przerabiam sobie ksiazke PHP5 zaawansowanie programowanie na temat programowania obiektowego i natknalem sie na taki fragment kodu :

  1. <?php
  2. foreach($band->getMusicians() as $musician) {
  3.        echo "Muzyk ".$musician->getName(). "<br>";
  4.        echo "to ".$musician->getMusicianType(). "<br>";
  5.        echo "w zespole grajacym ". $musician->getBand()->getGenre() . "<br>";
  6.        echo "o nazwie " .$musician->getBand()->getName(). "<br>";
  7. ?>


Chcialbym sie dowiedziec jak sa wywolywane tutaj funcke tego typu :

  1. <?php
  2. $musician->getBand()->getGenre()
  3. ?>


Wiem dobrze co sie dzieje jak wywoluje sie "pojedynczo" np tak:

  1. <?php
  2. $musician->getBand();
  3. ?>


Ale podwojnie to troche do mnie nie dociera. Moze ktos mi wytlumaczyc ? moge podac ewentualnie kod ktory klasy ale to chyba zbedne. Moze byc tez jakis inny przyklad niekoniecznie ten konkretny......
Wolfie
ta funkcja wyglada tak :

  1. <?php
  2. public function getBand() {
  3.            return $this->bandReference;
  4.        }
  5. ?>


snitch.gif
Wolfie
No to jest bardziej skomplikowane bo tutaj jeszcze sa jakies interface'y , moze przedstawie kod :

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


A co to settery i gettery to mi sie wydaje ze juz wiem , hermetyzacja tez nie jest mi obca, juz cos czytalem na ten temat
Wolfie
Nie no , co ten kod robi to ja wiem , tylko to wywolanie metod mnie zmylilo, no i z tego co widze to ta funkcja getBand nie zwraca zadnego obiektu smile.gif
Wolfie
Dziekuje za cierpliwosc i pomoc smile.gif

Ok, teraz zrozumialem wszystko smile.gif
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.