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

Review: creating polymorph classes using interfaces and abstract 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

Naturally, before I demonstrate how the pair of polymorph classes built in the previous tutorial can be used within the same script, it’d be helpful to show their respective definitions and reintroduce the signature of the interface and the abstract parent class from which they inherit both structure and functionality.

Here’s source code of the interface that declares a single method called “parseContent().” Take a look at it:

// define interface Parser

interface Parser

{

public function parseContent();

}

 

As I said before, the “Parser()” interface declares the signature of only one method, so there’s not much to be said about it for the moment. Let's look at the definition of the following abstract parent class, which encapsulates enough functionality to build generic HTML elements:

// define abstract class HtmlElement

abstract class HtmlElement

{

protected $id = 'myid';

protected $class = 'myclass';

protected $content = 'Default content for the HTML element';

 

// assign id attribute to HTML element

public function setId($id = '')

{

if ($id !== '')

{

$this->id = $id;

}

return $this;

}

 

// assign class attribute to HTML element

public function setClass($class = '')

{

if ($class !== '')

{

$this->class = $class;

}

return $this;

}

 

// set content for HTML element

public function setContent($content = '')

{

if ($content !== '')

{

$this->content = $content;

}

return $this;

}

 

// render HTML element (not implemented)

abstract function render();

}

As you can see, the above “HtmlElement” abstract class simply implements a few straightforward methods for outlining the behavior of generic HTML elements, so I won’t spend additional time explaining the details of its driving logic.

Instead, now that you hopefully recalled how this class functions, let me show you the definitions of the polymorph classes that implement the earlier “Parser” interface and are also derived from the parent class that you just saw.

Here they are:

class Div extends HtmlElement implements Parser

{

public function parseContent()

{

if ($this->content !== '')

{

$this->content = preg_replace(array('/(<html>|<body>)/', '/(<\/html>|<\/body>)/'), array('<div>', '</div>'), $this->content);

}

return $this;

}

// render div element

public function render()

{

return '<div id="' . $this->id . '" class="' . $this->class . '">' . $this->content . '</div>';

 

}

}

 

// define Paragraph class (subclass of HtmlElement class)

class Paragraph extends HtmlElement

{

public function parseContent()

{

if ($this->content !== '')

{

$this->content = str_replace(array('</p>','</p>)'), "", $this->content);

 

}

return $this;

}

// render paragraph element

public function render()

{

return '<p id="' . $this->id . '" class="' . $this->class . '">' . $this->content . '</p>';

 

}

}

Definitely, grasping the driving logic of the two polymorph classes isn’t rocket science, right? As you can see, they’re simple implementers of the pertinent “Parser” interface, in addition to being subclasses of the parent “HtmlElement.” This condition lets them take advantage of these two structures simultaneously. Not bad, eh?

So far, so good. At this stage, you're hopefully familiar with the way that these two polymorph classes do their business. So it’s time to develop some sample scripts that show how to use them in tandem for rendering HTML divs and paragraphs.

In the section to come, I’m going to create the first of those scripts, so read the following segment. I’ll be there, waiting for you.



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