Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
This is the forum to discuss the Wrox book Professional PHP5 by Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove; ISBN: 9780764572821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 31st, 2005, 03:34 AM
Registered User
 
Join Date: Aug 2005
Location: , , USA.
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to php4ever
Default Chapter 2 - simple example code

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>";
    }

?>


Reply With Quote
  #2 (permalink)  
Old September 2nd, 2005, 04:21 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mysticav
Default

Kindly can you explain me the following:

when to add instruments :

$bandMemberA->addInstrument(new Guitar("Gibson Les Paul"));

you are passing and guitar object, I just don't understand why when you received that obect in the addInstrument method, you just passed to the push_array function like this:
array_push($this->instruments, $instrument);

and it add the string "Gibson Les Paul" to the array ! from where did it get that string ? because it never mention an accesor method. It just simply the object.
it just passing an object, not a string, that's my doubt.


maybe my logic expects something like
array_push($this->instruments, $instrument->getName);

Kindly clarify this... Your script works perfect, just thatr I need to understand the logic, the theory. before I go further.




Reply With Quote
  #3 (permalink)  
Old September 2nd, 2005, 04:36 PM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mysticav
Default

Analyzing a little more, i concluded that the array $instruments from the Guitarist class stores objects as values, and not strings ?

am I right ?
if I'm right, well, the example is not so simple as they mention in the book. Because working with objects as values within an array, makes things a little complex. Anyway, At the end, OOP and complexity will always be together.

Reply With Quote
  #4 (permalink)  
Old September 12th, 2005, 08:28 PM
Registered User
 
Join Date: Aug 2005
Location: , , USA.
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to php4ever
Default

Sorry for my late reply but better later than never I hope.

Let's break things down, so it is abundantly clear what is going on.

Taking a look at the following line of code:

$bandMemberA->addInstrument(new Guitar("Gibson Les Paul"));

Let's read it from right to left.

Okay a new guitar object is instantiated and that new object is initialized with the string value of
"Gibson Les Paul". So, what does that mean? If you take a look at the constructor method for the guitar class (excuse my lack of uppercasing rt now) you will note that the constructor takes the string value and assigns it to the name property of the guitar object. Are you with me so far? You and I both read about accessor methods but there is no accessor method being used here since the work of setting the value of the name property is being done in the constructor vis a vis initialization -- it's another way of doing things.

So, what do we have now:

The addInstrument() method of bandMemberA is in this case a method of the guitarist class and the guitarist class is implementing the musician interface which has an addInstrument method. The only thing that the musician interface is insistent about is that the parameter for addInstrument must be an object of type instrument. That's it; the details have been left up to the guitarist class to implement.

What our method does is to add this instrument object to the array of instrument objects that our guitarist Jack Float plays. bandMemberA is also an object representing the guitarist Jack Float.


Hope this helps.
--php4ever





Reply With Quote
  #5 (permalink)  
Old October 30th, 2009, 07:40 AM
Registered User
 
Join Date: Oct 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Code works but I think this:

public function addMusician(Musician $musician){
array_push($this->musicians, $musician);
$musician->AssignToBand($this);
}

should be:

public function addMusician(Musician $musician){
array_push($this->musicians, $musician);
$musician->assignToBand($this);
}
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Very Simple code Tal1481 Beginning VB 6 9 January 29th, 2007 10:12 AM
Very Simple code Tal1481 Visual Basic 2005 Basics 1 January 25th, 2007 06:15 AM
Simple HTML code... rupen HTML Code Clinic 1 October 31st, 2006 05:36 PM
Chapter 15 simple-mailer.php doesn't work Corey Wirun BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 18th, 2003 03:13 PM



All times are GMT -4. The time now is 08:31 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.