Home arrow PHP arrow Page 3 - Building Polymorphs in PHP 5

Implementing the HtmlElement interface: building a div element class - PHP

You don't need to be working in a strictly-typed language like C to enjoy the benefits of polymorphism. You just need to achieve it at a different level. In this seven-part series, you'll learn how to use polymorphism in PHP 5.

TABLE OF CONTENTS:
  1. Building Polymorphs in PHP 5
  2. Implementing Polymorphism via interfaces: working with HTML element objects
  3. Implementing the HtmlElement interface: building a div element class
  4. Creating an HTML div in a few simple steps
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
January 06, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Now that I've defined a basic interface with the bare bones structure of a generic HTML element, it's time create a class that implements the set of methods declared by this interface. As you'll see in a moment, these methods will allow it to build polymorph objects very easily.

That being explained, here's the implementer class of the previous "HtmlElement" interface. Its function is to construct basic divs. Take a look at the partial source code of this class, please:

class Div implements HtmlElement

{

private $id = 'divid';

private $class = 'divclass';

private $content = 'Default content for the div';

 

 // assign id attribute to div element

public function setId($id = '')

{

if ($id !== '')

{

$this->id = $id;

}

return $this;

}

 

// assign class attribute to div element

public function setClass($class = '')

{

if ($class !== '')

{

$this->class = $class;

}

return $this;

}

}

As depicted above, the new "Div" class gives a concrete implementation to the "setId()" and "setClass()" methods defined at the top of the hierarchy, a process that demonstrates in a nutshell how to take advantage of Polymorphism in PHP 5 by using a simple interface.

However, as you may know, when a class implements an interface, it must do this with all of the interface's methods. That isn't the case with the above "Div" class, since no logic has been added yet to the "setContent()" and render()" methods.

Obviously, it's necessary to address this issue as soon as possible. Therefore, below I've included the complete definition of the "Div" class, this time implementing the aforementioned methods. Here it is:

class Div implements HtmlElement

{

private $id = 'divid';

private $class = 'divclass';

private $content = 'Default content for the div';

 

 // assign id attribute to div element

public function setId($id = '')

{

if ($id !== '')

{

$this->id = $id;

}

return $this;

}

 

// assign class attribute to div element

public function setClass($class = '')

{

if ($class !== '')

{

$this->class = $class;

}

return $this;

}

 

// set content for div element

public function setContent($content = '')

{

if ($content !== '')

{

$this->content = $content;

}

return $this;

}

 

// render div element

public function render()

{

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

 

}

}  

Certainly, you'll have to agree with me that the definition of the "Div" class now looks much clearer. The corresponding "setContent()" and "render()" methods have been specifically implemented to construct an HTML div element.

Despite the simplicity of this sample class, it's useful for showing how easy it is to build polymorph classes in PHP 5 by inheriting the set of methods declared by a certain interface.

So far, so good. Now that you've hopefully grasped the underlying logic of the earlier "Div" class, I'm going to set up an example that demonstrates the actual functionality of the class.

Bearing that idea in mind, in the following section I'm going to develop that example for you, which will be an adequate conclusion for this first part of the series.

Now read the next 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: