PDA

View Full Version : PHP Event Bindings



evildrummer
September 30th, 2008, 06:54 AM
So now that PHP 5.3 supports event binding I thought I would play about with creating a PHP event binding system. The syntax is similar to jQuery. Source hosted at http://github.com/stuartloxton/php-events/.

Example


<?php
include('index.php');

class database extends EventHandler {

function connect() {
// Do connection code
$this->connected();
}

function query() {
// make code;
$success = true;
if($success) {
$this->querySuccess(array('test', 'woop', 'success'));
} else {
$this->queryError();
}
}

}

$db = new database;

$db->connected(function() {
define('DB_CONNECTED', 'true');
echo '<h1>Connected</h1>';
return Event::unbind();
});
$db->querySuccess(function($e) {
echo '<h1>Query Returned</h1><pre>';
return Event::stop();
echo '</pre>';
});
$db->queryError(function() {
header('Location: /error/oops/');
exit;
});
$db->connect();
$db->query();
$db->connect();
?>


This would firstly create the class database to have event handling functions. It then binds a function to run on connected status. A function for query Success and query Failure.

When the Database is connected the connect functions are triggered. however one thing to point out is that in the binded function for connect it returns Event::unbind() which then unbinds it from the event so isn't run the second time the DB is connected.


I'm still doing a lot of work on it but thought I would post it here to see what you guys thought.



It only runs on PHP5.3 and above, otherwise the syntax would look like this:


// PHP < 5.3
$class->event(create_function('$e', 'define("SOMETHING", "10"); echo "something";'));
// PHP >= 5.3
$class->event(function($e) {
define('SOMETHING', '10');
echo 'something';
});

Templarian
September 30th, 2008, 11:17 AM
When is 5.3 suppose to be released?

Nice work so far, I'll probably wait till its on my server till I mess with it.

//edit, nvm its released with in the coming weeks (site says late September/October).

hl
September 30th, 2008, 02:35 PM
I'm not entirely clear about what event binding is/what's going on in the above example. Can someone elaborate, please?

evildrummer
September 30th, 2008, 05:16 PM
Basically it allows you to queue functions to run when you need them. So in my example the first bit just creates a dummy PHP class that extends the EventManager class (this then gives it the ability to bind events). The next bit is event binding.

Example:


// Binds a function to the connected event
$db->connected(function($e) {
echo '<h1>Connected</h1>';
});


All this does is allow you to run extra code when you need it and only when you need it. In my example I set a constant when it's connected. This means I can use the same DB class but for a different site or section have different events.

I'm not that great at explaining the concept, but after a while some people find it useful binding functions to things.

insaniak
September 30th, 2008, 05:43 PM
My favourite example that I found of events bindings (this time in Javascript) was that you can bind an event to the 'AJAX-Loading' function, i.e., you can make a function get called whenever anything calls the AJAX loading function. You could use something similar to display the 'Loading' text in the top left as seen on Google.
You don't have to ever call the bound function, it just calls whenever the thing it is bound to runs.

Mmmm, that might help? If you don't know js though probably not...

Wilhelm Murdoch
September 30th, 2008, 08:22 PM
It operates much like an observer pattern does. It's great that this is now built into PHP, but don't count on this build becoming widely used and production worthy anytime soon. I'm pretty stoked about this build, though. Wrote a nice article on it here (http://www.thedrunkenepic.com/home/articles/awesome-php-updates/) if anyone is interested.