HomePHP Creating a Subclass for Building Polymorphs in PHP
Creating a Subclass for Building Polymorphs in 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.
Now that you know what to expect from this set of articles, it’s time to summarize the topics covered in the last one. In that part of the series I started developing a basic example to demonstrate how to build a couple of polymorph classes that inherited most of their functionality from a base abstract class.
To extend this explanation, the aforementioned base class encapsulated most of the logic required for creating generic HTML elements. This made it very easy to derive a subclass from it and provide this child entity with the ability to render basic HTML divs.
Of course, it’s also fair to note that this example in its current state doesn’t clearly show how the class responsible for displaying divs on screen really behaves as a polymorph structure. However, if another child class is spawned from the corresponding parent, and it does display a different behavior when one of its methods gets called, then it’d be easy to demonstrate the polymorph nature of these classes, right?
Well, that’s exactly what I’m going to do in the next few lines For this concrete example, the brand new subclass that I plan to build next will be tasked with creating some HTML paragraphs.
Now, to learn how this will be done, click on the link below and start reading!