Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Polymorphism

polymorphism

  • Please log in to reply
2 replies to this topic

#1 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 02 February 2010 - 03:36 AM

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.

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.


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.

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

<?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

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.
  • 0
int coffeePerDay = 10; // need to cut down!!!
Codingfriends

#2 mnmtanish

mnmtanish

    CC Newcomer

  • Just Joined
  • PipPip
  • 22 posts

Posted 05 February 2010 - 10:50 AM

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)
  • 0

#3 genux

genux

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 76 posts

Posted 05 February 2010 - 11:24 AM

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.
  • 0
int coffeePerDay = 10; // need to cut down!!!
Codingfriends





Also tagged with one or more of these keywords: polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download