Home arrow PHP arrow Page 3 - Factoring (X)HTML Objects with Late Static Bindings in PHP 5.3

Defining generic (X)HTML objects - PHP

In this fifth part of a six-part series on late static bindings, I define some sample classes which are responsible for creating some block-level (X)HTML objects, such as simple divs and paragraphs. Once we get these classes up and running, we'll be ready to test the factories created in the previous part and see if they’re actually as functional as they look.

TABLE OF CONTENTS:
  1. Factoring (X)HTML Objects with Late Static Bindings in PHP 5.3
  2. Review: using Late Static Binding in the object scope
  3. Defining generic (X)HTML objects
  4. Building some block-level (X)HTML objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
May 25, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Considering that the main task of the concrete factory that you just saw in the previous section is to create a couple of block-level (X)HTML objects, it’s necessary to define the classes that will originate the objects. With that in mind, below you'll see the definition of a whole new abstract class that models the structure and partial functionality of generic HTML objects. Here it is:

(HtmlElement.php)

<?php

abstract class HtmlElement

{

    protected $_class = '';

    protected $_id = '';

    protected $_content = '';

   

    // constructor

    public function __construct(array $options = array())

    {

        if (isset($options['class']))

        {

            $this->setClass($options['class']);

        }

        if (isset($options['id']))

        {

            $this->setId($options['id']);

        }

        if (isset($options['content']))

        {

            $this->setContent($options['content']);

        }

    }

     

    // set the 'class' attribute for the HTML element

    public function setClass($class)

    {

        if (is_string($class))

        {

            $this->_class = $class;

        }

        return $this;

    }

   

    // get the assigned 'class' attribute

    public function getClass()

    {

        return $this->_class;

    }

     

    // set the 'id' attribute for the HTML element

    public function setId($id)

    {

        if (is_string($id))

        {

            $this->_id = $id;

        }

        return $this;

    }

   

    // get the assigned 'id' attribute

    public function getId()

    {

        return $this->_id;

    }

   

    // set the content for the HTML element

    public function setContent($content)

    {

        if (is_string($content))

        {

            $this->_content = $content;

        }

        return $this;

    }

   

    // get the content of the HTML element

    public function getContent()

    {

        return $this->_content;

    }     

    // implemented by concrete subclasses

    abstract public function render();       

}

Put in a simple way, the above “HtmlElement” class implements a few getters and mutators (aside from its constructor, of course) which retrieve and assign values to the “class” and “id” attributes of a skeletal HTML element. Besides, the element’s contents can be easily handled in a similar fashion via the “setContent()” and “getContent()” methods respectively.

For obvious reasons, the “render()” method has been declared abstract, since it should be implemented by concrete subclasses that render specific HTML elements. Therefore, now that you know how this abstract class does its thing, it’s time to start spawning a couple of child classes, which will be tasked with creating some trivial divs and paragraphs.

This process will be discussed in depth in the following section, so jump ahead and read the lines to come, please.         



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