Home arrow PHP arrow PHP Creating HTML Widgets

PHP Creating HTML Widgets

In this third part of a series I develop another sample class, which will use the Object Value design pattern to create immutable HTML div objects in a few easy steps. The pattern's implementation in this case will be a bit more interesting than usual -- it will use the PHP built-in type hinting feature to define a method that returns a new div object to client code, while keeping the immutability of the originating object intact.

  1. PHP Creating HTML Widgets
  2. Adding behavior to the Div class with a method for nesting Div objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
November 09, 2010

print this article



Even though at first glance it looks like an obscure and esoteric principle not worth analyzing in detail, the Value Object design pattern is a simple paradigm that permits you to generate, that is, objects whose equality is measured by the values assigned to their fields and not on a unique identity.

Unlike other programming languages, PHP doesn’t package by default a lot of native value objects, with the exception of the ones that can be spawned from its built-in ArrayObject and DateTime classes. The good news, though, is that creating them is a extremely easy process that you can tackle even if you have only an average background in the object-oriented approach. To demonstrate how simple it is to build immutable value objects using a custom class, in previous parts of this series I went through the development of a basic one, whose primary responsibility was modeling immutable URL objects.

By far, the most relevant facet of this construction process was the implementation of a method within the class, which was tasked with appending additional arguments to its existing query string. Considering the immutable nature of the class, the method acted like a simple factory which returned new URL objects to client code, in this way preserving the values assigned initially to the class’ fields.

As you may have already realized, the creation of immutable value objects isn’t limited to modeling well-formed URLs, as the process can easily be extended to other types of objects as well. So, since my goal here is to demonstrate the functionality of the Value Object pattern from a practical point of view, in this penultimate tutorial of the series I’m going to define another basic class, which will be charged with building a few immutable HTML widgets, and more specifically div elements.

Are you ready to learn the details of this process? Then keep reading.

Getting started building an immutable HTML widget class

As I said in the introduction, my purpose in this tutorial is to build a class capable of building immutable div objects. In keeping with this, I'll start by defining a contract for creating generic HTML widgets. Obviously, this can be easily achieved with an interface, so below is the definition of the one that I plan to use. Look at it, please:



interface HtmlWidgetInterface
    public function render(); 

Well, to be frank there’s no much that can be said about the above “HtmlWidgetInterface.” It declares a method called “render(),” which naturally must be provided with a concrete definition by all the classes implementing the interface in question.

With this simple contract already set, the next step is to create a class responsible for spawning the immutable div objects just mentioned. This class is not surprisingly called “Div,” and its source code is shown below:


final class Div implements HtmlWidgetInterface
     private $_content;
     // constructor
     public function __construct($content)
         if (!is_string($content) || empty($content)) {
             throw new HtmlWidgetException('The content assigned to the div element must be a non-empty string.');
         $this->_content = $content;
     // get the content of the div element
     public function getContent()
         return $this->_content;
     // render the div element
     public function render()
         return '<div>' . $this->_content . '</div>';




class HtmlWidgetException extends Exception{}

That was simple to code and read, wasn’t it? Similar to the examples developed in prior parts of this series, the brand new “Div” class doesn’t expose any setter methods to client code, in this way making it possible to create immutable div objects. In addition, the class concretely implements the “render()” method declared by the pertinent “HtmlWidgetInterface” interface, thus fulfilling the contract as expected.

So far, so good. At this point, if you give the “Div” class a try, it should work decently well, as each call to its “render()” method would effectively render a basic HTML div per object being created. However, in its current incarnation the behavior of the class is actually very limited; it’d be desirable to provide it with an extended functionality. In line with this idea, in the next section I’m going to add to the class a new method, which will be capable of rendering nested divs.

If you’re anything like me, at this moment you’re wondering how this will be done without affecting the class’ immutability, right? Well, to find out the answer to your question, you’ll have to click on the link below and read the following segment. 

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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