+ Reply to Thread
Results 1 to 3 of 3

Thread: Polymorphism

  1. #1
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Polymorphism

    I cannot seem to find a php tutorial about polymorphism so thought that I would .

    Polymorphism means that you are deriving from a base class to create a new class, and the polymorphism is when you derive from the base class and implement a interface functions, so that any derived class will have to these functions implement so if you call a function that is defined by the interface to be present, you know it will be implement in some forum.

    The basics of creating a interface for the class to implement.
    Code:
    interface Animal {
       public function 
    printName();

    there is no function code, and if you want to implement the interface with your class you use the syntax "implements" on the class definition line e.g.

    Code:
    class Cat implements Animal 
    {... 
    and if you tried to implement the interface Animal without actually implementing the function printName from the Animal interface, you will get a error like.

    Code:
    Fatal error: Class Cat contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Animal::printName)
    So the printName method will have to be implement for the PHP code to be "compiled".

    Here is fuller code to hopefully explain abit more

    Code:
    <?php
    interface Animal
    {
      public function printName();
    }
     
    class Cat implements Animal
    {
          public function printName()
          {
    	echo "Cat class\n";
          }
    };
     
    class Dog implements Animal 
    {
          public function printName()
          {
    	echo "Dog class\n";
          }
    };
     
    $animals = Array(
        new Cat(), new Dog()
        );
     
    foreach ($animals as $a)
    {
        $a->printName();
    }
     
    ?>
    and the output would be

    Code:
    Cat class
    Dog class
    polymorphism is great, because you will always know if you call a class function that implements a interface you will know that the class will have that function.
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    mnmtanish is offline Newbie
    Join Date
    Sep 2009
    Posts
    23
    Rep Power
    0

    Re: Polymorphism

    Just asking, I knew of Interfaces but still I haven't used it, even once.
    Are there any practical use with Interfaces?
    (except software security hardening)

  4. #3
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: Polymorphism

    Mnmtanish, I would say that is mainly it.

    It is just better programming style really and software development.

    Of course things like polymorphism is great for it.

    I normally use interfaces ( or pure virtual functions in cpp) purelly because itakes the bigger project easier to follow and also if you are working with a remote company for example, following software development standards means that anyone else can just pick it up and follow on from where I have left off.
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Polymorphism
    By Turk4n in forum Java Tutorials
    Replies: 20
    Last Post: 08-01-2010, 07:38 PM
  2. Polymorphism help ..!!
    By R3.RyozKidz in forum Java Help
    Replies: 1
    Last Post: 01-22-2010, 09:43 PM
  3. polymorphism
    By Siten0308 in forum C# Programming
    Replies: 13
    Last Post: 03-24-2009, 02:57 AM
  4. Please explain Polymorphism
    By Patrick in forum C and C++
    Replies: 3
    Last Post: 10-09-2007, 06:09 PM
  5. polymorphism
    By Nightracer in forum General Programming
    Replies: 6
    Last Post: 07-26-2006, 07:53 PM

Tags for this Thread

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