Friday , 23 June 2017
Home / Programming / PHP / Polymorphism – PHP OOP Tutorial
Polymorphism
Polymorphism

Polymorphism – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

Polymorphism is a pretty big word, but it’s actually a simple concept to understand. Here’s how it’s officially defined.

Polymorphism is a programming language feature that allows values of different  data types to be handled using a uniform interface.

That may seem a little bit confusing to most of you new programmers. Luckily, polymorphism is a light switch concept meaning once you understand it, you know it forever.

So, what is polymorphism?

Basically, let’s say we had an apple, banana, and orange class. They’re all different, but they all fall under the same category as fruit. So, it would make sense to create an interface named fruit and have all these classes implement it. That’s what polymorphism is. It’s a bunch of classes with different functionality and structure, but all use the same interface.

Why would you use polymorphism in the first place?

Well, let’s go back to our fruit example. Let’s say for a moment that our fruit class was not an interface, but a class instead. Let’s also say that the apple, banana, and orange class didn’t exist.  If we wanted to create a new fruit, we would have to use the fruit class. The fruit class is a very broad class because there’s many kinds of fruit. Every fruit is different and they taste different, but they’re all categorized under fruit.

In order to eat an orange, you have to peel the orange and then eat it, while you can just go ahead and bite an apple. Because of this,  you have to define conditional statements to check whether or not you have to peel the fruit to eat it. This makes one huge messy class that ‘s not very efficient. Instead of creating a class for a broad subject, you can create an interface for a bunch of different classes that run differently.

While the classes may run differently, they have some common characteristics. Back to our fruit example, all fruit have to be grown. That’s what they have in common. Other than that, they’re all different sizes and shapes and texture. A pineapple for example, has spikes surrounding it.

So, if we had one universal class, why would an apple need a spike method? It would be better to create an interface with classes that implement it than have one class for everything. This is the power of polymorphism.

Conclusion

Hopefully you got it. To sum it all up, polymorphism is what unifies a bunch of classes with one interface. To learn more about polymorphism, then go here

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

About Jasko Koyn

Check Also

Static Methods and Properties

Static Methods and Properties – PHP OOP Tutorial

Let’s learn how to create static methods and properties. Anything that’s static is basically something …

9 comments

  1. Thanks, I really understand polymorphism in your explanation. Simple but rock… I already understand interface and abstract. Thanks again dude for a very simple explanation.
  2. thank you so much you really make me understand so easily the example of fruits were very and helpful to understand so easily .i appreciate your work keep it up .
  3. good explanation
  4. Best explanation of Polymorphism that i’ve seen on the internet. Thank you!
  5. I don’t think I’ve seen anyone use fruit to describe polymorphism yet. Now I’m hungry :-). Some code examples seeing these concepts in action would be awesome. Nice work Jasko!
  6. Very good explenation, Thanks keep it up
  7. Very good explanation. I think you should give an code example.
  8. Good explanation in simple terms !!!

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *