Event driven programming in PHP - Building flexible controls





Download the demo code

Have a look at the following crude implementation of a datagrid control:

 
class DataGridRow
{
    public $cells;
    function DataGridRow()
    {
        $array = func_get_args();
        $this->cells = $array[0];
    }
}
 
class DataGrid
{
    public $rows;
 
    public function AddRow()
    {
        $this->rows[] = new DataGridRow(func_get_args());
    }
 
    public function Render()
    {
        $html='<table border="1">';
        foreach($this->rows as $row)
        {
            $html.='<tr><td>'.implode('</td><td>', $row).'</td>
                        </tr>';
        }
        echo $html.'</table>';
    }	
}
 

Lets suppose you created a killer datagrid (unlike this one), how would you approach scenarios where developers need to change very specific items in the datagrid? Ordinarily things that are very specific to their solution/problem they're working on?

Better yet, lets say this is your commercial product and you need your component to be flexible. You dont want people to struggle and dig around in 10000 lines of code to have to bend it to their will (time is afterall money).

You can always add new methods, thereby providing the required features. Ultimately adding a lot of bloated unneeded functionality - thats got absolutely nothing to do with the grid in the first place.

The bottom line is its very difficult to cater for every imaginable scenario to start with. Highlighting the usefulness using events.

The following example takes our fantastic grid and extends it a little bit, using an event.
 
class DataGrid
{    
    public $rows;
    public $ondatabind;
 
    public function DataGrid()
    {
        $this->ondatabind = new Event();
    }
 
    public function AddRow()
    {
        $this->rows[] = new DataGridRow(func_get_args());
    }
 
    public function Render()
    {
        $html='<table border="1">';
        foreach($this->rows as $row)
        {
            $this->ondatabind->Raise($this, $row);
            $html.='<tr><td>'.implode('</td><td>', $row).'</td>
                    </tr>';
        }
        echo $html.'</table>';
    }	
}
 

What we've done here is provide a way to add some logic that must be executed while rendering is taking place - without making any changes to the datagrid class.

Suppose this is your grid, and you need to highlight every PG13 age restriction in red.

Forrest GumpPG13
Ice AgePG
Star WarsPG13

 
// Instantiate the control
$Grid = new DataGrid();
 
// Add some items to the grid (Generally from MySQL)
$Grid->AddRow("Ice Age","PG");
$Grid->AddRow("Star Wars","PG13");
$Grid->AddRow("Forrest Gump","PG13");
 
//Subscribe to the event
$Grid->ondatabind->Subscribe("databindEvent");
 
// Event handler
function databindEvent($sender, $e)
{
    if ($e->cells[1] == "PG13")
    {
        $e->cells[1] = '<span style="background-color:#550000;color:#FFFFFF"">'.$e->cells[1].'</span>';
    }
}
 

You will end up with something like this:

Forrest GumpPG13
Ice AgePG
Star WarsPG13

Lets have a look at a more traditional use of events.





No Entries Found

Post comment

Name *
Email
Title
Body *
Security code
*
* Required fields

Latest Articles

Top 5 Articles

Programming humor


Collection of funny programming articles
2006-10-08 14:23:43

How to create your own RSS Reader


It is very simple creating your own rss reader, the following article looks at a few methods of doing this.
2008-06-23 13:18:25

Javascript Reference: Dropdown


A quick reference about working with dropdown boxes (select element) in javascript.
2007-02-17 16:36:41

PHP: Snippets


Collection of PHP snippets
2010-05-22 00:06:45

Event driven programming in PHP


An article looking at adding some kind of event driven model to PHP 5
2008-07-28 12:48:09