Posts tagged: class

Zend Framework: Render If Exists

By , Sunday 12th December 2010 4:19 pm


This is a quick post to discuss the rather simple view helper I created for rendering a Zend Framework style view file only if it exists. Generally asking the code to render a file which doesn’t exist will throw an exception. Therefore I created a wrapper for the Zend_View::render() method which determined whether the file exists and if so renders, otherwise simply returns an empty string.
Continue reading 'Zend Framework: Render If Exists'»

PHP Design Patterns – Observer Pattern

By , Tuesday 29th December 2009 10:02 pm

I’ve been reading Head First Design Patterns recently and have decided to write some of the patterns as PHP examples for my own benefit. The first one that I’ve decided to code up is the Observer Pattern. The formal definition of the Observer Pattern is:

The observer pattern (a subset of the asynchronous publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

As systems become more loosely coupled making sure that when an event happens all systems that require knowledge of these updates are informed. For example, a blog post, after saving a post we may need to update a search engine (e.g. Lucene), update our sitemap, tags, email subscribed users, etc. The observer pattern allows developers to add additional listeners without editing their observable object. By injecting observers (i.e. a search engine update observer, a sitemap generator, etc) into a subject (i.e. blog post editing system) we can allow the it to perform all the necessary updates without any changes.

Continue reading 'PHP Design Patterns – Observer Pattern'»

Html Table Generating Class (HtmlTable.php)

By , Tuesday 5th May 2009 9:11 pm

I required to generate a HTML table from PHP data for a project that I was working on. Unfortunately after a bit of Googling I couldn’t really find anything that was suitable so I decided to create my own.

I realise that this may not be the best table generating class ever and there’s probably a few bugs still in it, but I’d prefer to share and if anyone wants a better version I’ll look into it :) I originally wanted to create a table built up of seperate objects e.g. cell object, row object (built up of cell objects), but I realised the overhead on this would be HUGE! So I’ve gone back to an array setup.

The class will calculate the maximum number of columns used in the header, footer, or body and write out that number of columns. Headers, Footers, and the Caption are optional. Attributes are made up of an array where the key is the attribute name and the value is the attribute value. Attributes are applied to table tag (on table creation – class initialisation), table rows (array[row][attribute]), and table cells (array[row][column][attribute]) this applies for headers, footers, and the table body.


I always find the best way to look at something is to use an example so here we go:

$table = new HtmlTable( array('class' =>'sortable',
                'style' => 'width: 550px;'));
$header[0][0]['content'] = 'ID';
$header[0][1]['content'] = 'Title';
$header[0][2]['content'] = 'Date';
$header[0][3]['content'] = 'Site';

$i = 0;
foreach ($newsList AS $newsItem) {
$itemLink = "{$newsItem['title']}";
$tableBody[$i] = array(0 => array('content' => $newsItem['id'],
'attributes' => array('style' => 'font-weight: bold;')),
1 => array('content' => $itemLink),
2 => array('content' => $newsItem['date_posted']),
3 => array('content' => $newsItem['site']) );
echo $table->drawTable();
ID Title Date Site
9 News 1 2009-04-04 10:40:00 site name
10 News 2 2009-04-04 12:44:11 site name

And the HTML looks like this:

<table class = "sortable" style = "width: 550px;" >
	<tr >
		<th >ID</th>
		<th >Title</th>
		<th >Date</th>
		<th >Site</th>
	<tr >
		<td style = "font-weight: bold;" >9</td>
		<td >News 1</td>
		<td >2009-04-04 10:40:00</td>
		<td >site name</td>
	<tr >
		<td style = "font-weight: bold;" >10</td>
		<td >News 2</td>
		<td >2009-04-04 12:40:00</td>
		<td >site name</td>


If you have any comments please make them below, it would be great to hear some. If you end up using it also let me know and I’ll extend it in the future :)

Panorama Theme by Themocracy

4 visitors online now
1 guests, 3 bots, 0 members
Max visitors today: 7 at 06:00 pm UTC
This month: 16 at 04-02-2017 11:08 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC