Home arrow PHP arrow Page 2 - Polymorphs in PHP: Using Interfaces and Abstract Classes to Construct HTML Paragraphs

Review: building a polymorph object using an abstract class and an interface - PHP

In this penultimate part of a seven-part series on building polymorphs in PHP 5, I demonstrate how easy it is to build polymorph objects that merge the functionality of abstract classes and the structure of interfaces. The entire creation process is simple enough that you shouldn’t have major problems grasping its underlying logic.

TABLE OF CONTENTS:
  1. Polymorphs in PHP: Using Interfaces and Abstract Classes to Construct HTML Paragraphs
  2. Review: building a polymorph object using an abstract class and an interface
  3. Displaying HTML paragraphs using an object-oriented approach
  4. The polymorph class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 19, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In the preceding part of this series I went through the development of a polymorph class that inherited its methods from an abstract class and from a single interface. As a review, below I included the complete source code corresponding to this example.

Study the following code sample, please:

// define interface Parser

interface Parser

{

public function parseContent();

}

 

 

// 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();

}

You shouldn't have much trouble understanding the purpose of defining the previous “Parser()” interface and the complementary “HtmlElement” abstract class. As shown above, when properly combined, these entities provide both functionality and structure for building simple HTML elements that can be easily rendered on the browser.

However, to understand more clearly how these sample entities can be used for achieving Polymorphism in a simple manner, it’s necessary to define a class that implements the inherited “parseContent()” method. The following “Div” class performs this task in only one go:

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>';

 

}

}

There you have it. In this case, the “Div” class adds concrete logic to the “parseContent()” method, so it can be used for stripping unwanted tags from the contents assigned to div elements, such as <html> and <body>. Of course, it’s possible to make this method more complex, but for the moment I’ll keep it  simple.

Now that you've learned (or recalled) how the above “Div” class does its thing, here’s a short script that shows how to use it in a concrete case:

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

 ->parseContent()

->render();

While this code sample does demonstrate how to use the “Div” class for constructing a simple HTML div, it admittedly comes up short in showing the class’s polymorph nature. What’s wrong with this example? Well, nothing really, but to prove that this class is a polymorph structure, it’s necessary to build another one that displays a different behavior for the same “parseContent()” method.

However, building that new class will be covered in the next section. So click on the link below and read the next few lines.



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