Home arrow PHP arrow Page 3 - Using Different Objects in the Same Script for Polymorphs in PHP

Rendering HTML divs and paragraphs within the same script - PHP

Welcome to the final part of a seven part series on building polymorphs in PHP. In a friendly fashion, this series gets you started constructing polymorph classes specifically in PHP 5. It shows you how to accomplish this by using first interfaces, then parent classes, and finally a proper combination of both.

TABLE OF CONTENTS:
  1. Using Different Objects in the Same Script for Polymorphs in PHP
  2. Review: creating polymorph classes using interfaces and abstract classes
  3. Rendering HTML divs and paragraphs within the same script
  4. Passing a different argument to the parseContent()method of the polymorph classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 20, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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