SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: hi

  1. #1
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hi

    PHP Code:
    class Animal
    {
          
    // This is the base animal class, which Cat and Dog derive from
            
            
    var $name;

            function 
    setName($name)
            {
                    
    $this->name $name;
            }

            function 
    makeSound()
            {
                    echo 
    "...\n";
            }
    }

    class 
    Cat extends Animal
    {
            
    // Cat inherits $name, setName() and makeSound() from Animal.
            
    function makeSound()
            {
                    
    // Here we override makeSound()
                    
    echo "Meow!\n";
            }
    }

    class 
    Dog extends Animal
    {
            function 
    makeSound()
            {
                    echo 
    "Woof!\n";
            }
    }

    class 
    Squirrel extends Animal
    {
            
    // Since we don't define anything here, Squirrel will just 
    inherit everything from Animal and leave it at that
    }

    $a = new Animal;
    $c = new Cat;
    $d = new Dog;
    $s = new Squirrel;

    $a->makeSound(); // Outputs "..."
    $c->makeSound(); // Outputs "Meow!"
    $d->makeSound(); // Outputs "Woof!"
    $s->makeSound(); // Outputs "..."

    $animals = array($c$s$d$a);
    foreach(
    $animals as $animal)
    {
            
    $animal->makeSound(); // Outputs "Meow!", "...", "Woof!" and 
    "..."

    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Hi.



    Do you have a question?

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Next step is to make sure Animal cannot be called, only implemented. Someone already converted the Head First Design Pattern example of a Strategy Pattern (featuring animals, btw) to PHP, it may expand your horizon.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •