HomePHP Page 3 - Creating a Subclass for Building Polymorphs in PHP
Building another polymorph subclass - PHP
Welcome to the fourth installment of an article series on building polymorphs in PHP. Made up of seven tutorials, this series attempts to teach you through numerous, comprehensive code samples how to create polymorph objects. You'll see how to do it using interfaces, then abstract classes, and finally a combination of both.
As I explained in the section that you just read, to demonstrate that many of the subclasses derived from the parent “HtmlElement” can be considered true polymorph entities, it’s necessary to build a class that behaves radically different when its “render()” method gets called.
With that requirement in mind, below I included the definition of this brand new subclass. It's charged with building HTML paragraphs. Look at it, please:
// define Paragraph class (subclass of HtmlElement class)
Were you expecting to be confronted with a complex and lengthy class? Well, fortunately for you and me this isn’t the case here. The task that the “Paragraph” class performs is constructing HTML paragraphs via its inherited “render()” method.
Now that this new subclass has been completely defined, it should be clear for you to see its polymorph nature, particularly when compared with its sister “Div.” These two child classes are of the same “HtmlElement” type, but each of them acts radically different when rendering a specific HTML element.
At this point, you’ve hopefully grasped the logic that stands behind building polymorph objects using an abstract class in PHP 5. But, there’s still one thing that remains undone -- developing a script that shows how to use the previous “Paragraph” class.
That script will be built in the last part of this tutorial. Therefore, click on the link that appears below and read the next few lines.