Object-oriented HTML Generation In PHP
Object-oriented HTML Generation In PHP
Written by Alex Armstrong   
Tuesday, 09 September 2014
Article Index
Object-oriented HTML Generation In PHP
Object-oriented approach


If you have heard of or used JQuery you probably know that it is a Javascript library that allows you to manipulate the DOM on the client side. PHPQuery is a PHP version of JQuery intended for use on the server side. In most cases PHPQuery and JQuery are used to analyse an existing DOM, but there are big advantages to using them for a slightly less obvious task - generating HTML via the DOM.

Using PHPQuery has an even bigger advantage if you are already using JQuery on the client side - after all why learn two different approaches.

You can download PHPQuery from:


where you will also find documentation and a community site.

Once you have the PHP library installed you can use it to generate the button example very simply:

$doc = phpQuery::newDocument();
  ->attr('value','My Button');
echo $doc;

The steps in the program are very simple. First we open a new document, then create a PHPQuery button object, complete with attribute settings, append the object to the DOM, and finally generate the HTML.

It really is this easy and notice the way that the method calls to set the attributes can be chained together - just like JQuery.  If you already know JQuery than the only extra information you really need is the fact that the pq function behaves like the Javascript JQuery or $ function.

Of course there is more to PHPQuery than generating simple buttons and you can use it to process a DOM in complex ways in an efficient style that characterises JQuery. It even supports server side Ajax using a similar set of method calls!

The big problem is that PHPQuery also seems to have died as a project. You can still download the code but nothing seems to be happening any more - shame. 


The future

Generating HTML is PHP's main role in life and as such it is surprising that it does it so badly.

Quoting HTML tags is an easy way to get beginners started, but the horrible mix of HTML and PHP that results when you try to generate a real web page is far from good.

PHP needs an official, i.e. built-in to the language system, object-oriented way of generating HTML.

Until such time you will have to create your own solutions if you want to go down this road. 


Related Articles

Ten minutes to PHP objects

PHP inner functions and closure

Advanced loops


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.



PHP Inner Functions And Closure

PHP inner functions and anonymous functions are a little strange to say the least. However, just because something is strange doesn't mean that it isn't useful. We take a close look at the way PHP fun [ ... ]

Peachpie Open Source PHP to .NET Compiler

Peachpie is a new open source PHP language to .NET compiler, which aims at full PHP 7 compatibility. Looking at it gave us the opportunity  to revisit the state of dynamic language interoperabili [ ... ]

Other Articles




or email your comment to: comments@i-programmer.info







Last Updated ( Tuesday, 09 September 2014 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.