Interesting that the authors deems the example on pp.39-41 "simple." Frankly, I think the editors/authors need to delete on p.41 the phrase "Although this example is fairly simple" and instead just talk about what noteworthy OOP aspect the example illustrates. That way no one need feel stupid in case he/she is new OOP and finds the example far from simple.
Okay, now for the good part, the code that works. Yes, there are errors in the book's source code but let me make your life a little bit easier and give you the code that works with an enhancement so the result is also going to be outputted according to good English grammar. There *still* is a place for a good conditional.
And, one last thing to note is that if you have an interface that has a method that takes a certain type of parameter, guess what? Any class that implements that interface is going to have to make certain that the method of its class however it works is going to have to have the same kind of parameter. See the example at php.net at:
http://www.php.net/manual/en/languag...interfaces.php.
Oh, and about the errata in the book's simple example, 1) cf the Band interface method addMusician with that of the RockBand class,
2) cf the Musician interface methods addInstrument and assignToBand with the those of the Guitarist class. 3) Also note that the original code shows $this-$bandReference twice in the Guitar class, namely methods getBand and assignToBand.
Try the following -- it works!
<?php
error_reporting(E_ALL);
interface Band {
public function getName();
public function getGenre();
public function addMusician(Musician $musician);
public function getMusicians();
}
interface Musician {
public function getName();
public function addInstrument(Instrument $instrument);
public function getInstruments();
public function assignToBand(Band $band);
public function getMusicianType();
}
interface Instrument {
public function getName();
public function getCategory();
}
class Guitarist implements Musician {
private $last;
private $first;
private $musicianType;
private $instruments;
private $bandReference;
function __construct($first, $last) {
$this->last = $last;
$this->first = $first;
$this->instruments = array();
$this->musicianType = "guitarist";
}
public function getName() {
return $this->first . " " . $this->last;
}
public function addInstrument(Instrument $instrument) {
array_push($this->instruments, $instrument);
}
public function getInstruments() {
return $this->instruments;
}
public function getBand(){
return $this->bandReference;
}
public function assignToBand(Band $band) {
$this->bandReference = $band;
}
public function getMusicianType(){
return $this->musicianType;
}
public function setMusicianType($musicianType){
$this->musicianType = $musicianType;
}
}// end class
class LeadGuitarist extends Guitarist {
function __construct($last, $first) {
parent::__construct($last, $first);
$this->setMusicianType("lead guitarist");
}
}// end class
class RockBand implements Band {
private $bandName;
private $bandGenre;
private $musicians;
function __construct($bandName) {
$this->bandName = $bandName;
$this->musicians = array();
$this->bandGenre = "rock";
}
public function getName(){
return $this->bandName;
}
public function getGenre(){
return $this->bandGenre;
}
public function addMusician(Musician $musician){
array_push($this->musicians, $musician);
$musician->AssignToBand($this);
}
public function getMusicians(){
return $this->musicians;
}
} // end class
class Guitar implements Instrument {
private $name;
private $category;
function __construct($name) {
$this->name = $name;
$this->category = "guitar";
}
public function getName() {
return $this->name;
}
public function getCategory(){
return $this->category;
}
}
// Test Objects
$band = new RockBand("The Variables");
$bandMemberA = new Guitarist("Jack", "Float");
$bandMemberB = new LeadGuitarist("Jim", "Integer");
$bandMemberA->addInstrument(new Guitar("Gibson Les Paul"));
$bandMemberB->addInstrument(new Guitar("Fender Stratocaster"));
$bandMemberB->addInstrument(new Guitar("Hondo H-77"));
$band->addMusician($bandMemberA);
$band->addMusician($bandMemberB);
foreach($band->getMusicians() as $musician) {
echo "Musician " .$musician->getName() . "<br>";
echo "is the " . $musician->getMusicianType() . "<br>";
echo "in the " . $musician->getBand()->getGenre() . " band<Br>";
echo "called " . $musician->getBand()->getName() . ".<br>";
$i = 0;
$cphrase = "and plays";
foreach($musician->getInstruments() as $instrument) {
if ($i) { $cphrase = "as well as ";}
echo "$cphrase the " . $instrument->getName() . " ";
echo $instrument->getCategory() . ".<br>";
$i++;
}
echo "<p>";
}
?>