Home arrow PHP arrow Page 4 - Building Polymorphs in PHP 5

Creating an HTML div in a few simple steps - PHP

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

As I promised in the previous segment, below I wrote a script that shows how to use the polymorph "Div" class to display a containing div on screen. Here's the corresponding code fragment:

// create new instance of Div class

$div = new Div();

// assign attributes and content for div element and display it on the browser

$div->setId('myid');

$div->setClass('myclass');

$div->setContent('This is the new content for the div.');

echo $div->render();

 

That was easy to code, wasn't it? Due to the intuitive API of the "Div" class, it's really simple to create first an instance of it, then assign some trivial values to its properties via the pertinent setter methods, and finally render the div element on screen.

In addition, since most of the methods of this class are chainable, it's feasible to rewrite the previous script a little bit more tightly, like this:

// create new instance of Div class

$div = new Div();

// assign attributes and content for div element and display it on the browser

echo $div->setId('myid')

->setClass('myclass')

->setContent('This is the new content for the div.')

->render();

There you have it. Thanks to the neat support offered by PHP 5 for working with interfaces, building polymorph classes is a straightforward process that can be tackled with minor effort.

As always, you're free to tweak all of the code samples shown in this tutorial. Doing so will surely help you grasp more quickly how to implement Polymorphism in PHP 5 using interfaces.

Final thoughts

That's all for the moment. In this introductory episode of the series, I demonstrated how easy and useful it is to implement Polymorphism in PHP 5 by way of interfaces. The driving force behind this approach is always Inheritance, even though, in this specific case, the previous "Div" class only inherits a set of generic methods.

In the next tutorial I'm going to develop another sample application that will use the same "HtmlElement" interface that you saw before, but this time for building some HTML paragraphs.

Don't miss the upcoming part!



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