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

Passing a different argument to the parseContent()method of the polymorph classes - 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

I’d like to finish this last part of the series by coding another simple example that shows how the “render()” and “parseContent()” methods that belong to the previous “Div” and “Paragraph” classes can behave radically differently, even when called in the context of the same script.

This particular hands-on example looks like this:

// 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('<body>This is the new content for the div.</body>')

->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>This is the new content for the paragraph.</p>')

 ->parseContent()

->render();

Hopefully with this final example, you’ll see for yourself that the “Div” and “Paragraph” classes shown previously are true polymorph structures. To demonstrate this concept more clearly, I modified the contents that are inputted into the “parseContent()” method of the classes, which will produce completely different results.

Feel free to edit all of the code samples included in this tutorial. Doing so will help give you a better grounding in building polymorph objects in PHP 5.

Final thoughts

It’s hard to believe, but we’ve come to the end of this series. Hopefully this group of tutorials has been insightful for you. You learned how to achieve Polymorphism in PHP 5, first by using interfaces, then via abstract classes, and finally by combining these two structures simultaneously.

At first glance, and particularly for developers taking their first steps in PHP-based web development, it seems that building polymorph objects is rather a pointless task. However, Polymorphism is indeed a powerful paradigm that will help you build solid, well-structured object-oriented applications.

See you in the next PHP development tutorial!



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