Without a doubt, the best way to understand the polymorph nature of the subclasses shown in the previous segment is by means of some functional code samples that show how those classes expose a different behavior when their respective “render()” and “parseContent()” methods are invoked within the same script. Below I included such a script, so I suggest you to look at it closely: // create new instance of Div class $div = new Div(); // assign attributes and content to div element and display it on the browser echo $div->setId('divid') ->setClass('divclass') ->setContent('<html>Testing the div class.</html>') ->parseContent() ->render();
// create new instance of Paragraph class $par = new Paragraph(); // assign attributes and content to paragraph element and display it on the browser echo $par->setId('parid') ->setClass('parclass') ->setContent('<p>Testing the paragraph class.</p>') ->parseContent() ->render(); Perhaps it’s unnecessary to clarify this, but if you run the above script on your own testing web server, you’ll see that a div and a paragraph will be outputted to the browser. Even though this particular example is rather simplistic, it shows in a nutshell how easy it is to construct a couple of polymorph classes by relying on an interface and an abstract class simultaneously. Are you not convinced yet of the real polymorph behavior of these sample classes? Well, of course you have the right to think that way. However, since my goal here is to dissipate all of your doubts regarding this subject, in the final section of this tutorial I’m going to create another example for you, so you can finally be convinced. Now, to see how this concluding example will be developed, click on the link that appears below and read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|