You don't need to be working in a strictly-typed language like C to enjoy the benefits of polymorphism. You just need to achieve it at a different level. In this seven-part series, you'll learn how to use polymorphism in PHP 5.
If you've ever developed applications with C, then you know that its support for Polymorphism is thorough, clear and well defined. Being a structured, strictly-typed language, C will let you define functions that have the same name and that also accept the same number of arguments. However, if the arguments taken by one function aren't of the same type as the ones taken by another, then it's assumed that these are different functions that will behave differently as well.
In the case of C, Polymorphism is achieved due to the strict nature of the language itself, which bring to us, as passionate PHP developers, an interesting question: if PHP is a weakly-typed language where functions are remarkably permissive regarding the arguments that they can accept (for good or for bad), how can Polymorphism be successfully achieved, then?
Well, since at the function level Polymorphism simply can't be implemented for the reasons that I gave before, it can be easily achieved at class level instead. Let me clarify this concept: if there are two classes that inherit a certain method from the same parent, but each of them give it a different implementation, then the objects derived from those classes will be considered polymorphs.
Essentially, in the case of PHP, Polymorphism is implemented through Inheritance. This concept can be taken even further with PHP 5, since it supports not only creating parent classes in a traditional way, but defining interfaces as well. Since one class can inherit methods from a base class, one or many interfaces, or both, PHP 5 provides developers with the ability to implement Polymorphism via regular Inheritance or via interfaces.
Definitely, this capability of PHP 5 to construct polymorphs using two different approaches can be really useful for developing object-oriented applications that exploit code reuse in a more efficient manner. Thus, in this set of articles I'm going to take a close look at achieving Polymorphism in PHP 5 through interfaces, and through the use of parent classes, so you can easily spot the differences between these two methodologies, and evaluate their pros and cons.
Now, it's time to get rid of the dull theory and start building polymorphs using PHP 5 interfaces. Let's go!