Home arrow PHP arrow Building Polymorphs in PHP 5

Building Polymorphs in PHP 5

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.

TABLE OF CONTENTS:
  1. Building Polymorphs in PHP 5
  2. Implementing Polymorphism via interfaces: working with HTML element objects
  3. Implementing the HtmlElement interface: building a div element class
  4. Creating an HTML div in a few simple steps
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 06, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: