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

Implementing Polymorphism via interfaces: working with HTML element objects - 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

Since the purpose of this introductory part of the series is to demonstrate how to implement Polymorphism in PHP 5 by means of interfaces, I'm first going to define a simple interface that will declare some generic methods for outlining the general structure and behavior of an HTML containing element.

Once you've seen the structure of this interface, I'll move on and code a new class that will implement all of its methods. That being said, here's the definition of the interface, which not surprisingly is called "HtmlElement." Check it out:

interface HtmlElement

{

// assign id attribute to HTML element

public function setId($id = '');

 

// assign class attribute to HTML element

public function setClass($class = '');

 

// set content for HTML element

public function setContent($content = '');

 

// render HTML element

public function render();

}

Certainly, it's easy to understand the rationale behind the above interface. As you can see, it declares only three public methods. The first two assign "id" and "class" attributes to a generic HTML element, and the last one can be used for displaying that element on the browser.

So far, there's nothing special about the way that this interface has been defined, right? However, here's where things become really interesting; the next step to taking advantage of Polymorphism is to create an implementer of the interface that specifically constructs a div element.

So, if you wish to learn the full details of how this new class will be created, then click on the link displayed below and read the section to come.



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