HomePHP 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.
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.
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!