Home arrow PHP arrow Page 4 - Using Abstract Classes to Build Polymorphs in PHP 5

Using the Div subclass - PHP

In this third part of a seven-part series, I demonstrate how to build polymorph objects using abstract classes in PHP 5. As you'll see, it's a pretty straightforward process that can be tackled with relative ease.

  1. Using Abstract Classes to Build Polymorphs in PHP 5
  2. Review: building polymorph objects using interfaces
  3. Implementing Polymorphism via abstract classes
  4. Using the Div subclass
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 12, 2010

print this article



As I expressed in the preceding segment, I’d like to end this tutorial by showing you how to use the “Div” class built previously, so you can see how simple this process can be.

Here’s an example script that creates an instance of the corresponding “Div” class and uses the “render()” method for displaying a div on the browser:

// 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('divid')->setClass('divclass')->setContent('This is the new content for the div.')->render();

There you have it. As seen above, it’s extremely easy to create and output a div on screen, thanks to the chainable interface of the “Div” class. Of course, this example script assumes that the definition of the base “HtmlElement” class has been previously included, so make sure to do that if you wish to try this example using your own testing web server.

Finally, feel free to edit all of the code samples included in this tutorial, so you can arm yourself with a more thorough background in building polymorph objects that rely on the use of abstract classes.

Final thoughts

In this third installment of the series, I demonstrated that building polymorph objects using abstract classes in PHP 5 is a pretty straightforward process that can be tackled with relative ease. Well, admittedly I’m halfway to completing the demonstration process; at this point I've built a subclass that inherits part of the functionality from its abstract parent and builds HTML divs.

You may be wondering what’s missing in this schema. It’s necessary to derive yet another class from the same parent that behaves differently when its “render()” method gets called.

This topic will be covered in depth in the upcoming tutorial, so now you don’t have any excuses to miss it!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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