OK - first changing the trigger method in the listener class above;
PHP Code:
function trigger () {
if (isset($this->get)) {
foreach ( $this->get as $eventName => $eventHandler ) {
$$eventHandler=& new $eventHandler;
$$eventHandler->respond();
}
} else {
// Do default handler if no events
$defaultHandler=& new DefaultHandler;
}
}
Note the variable variable now (like I said - haven't been tested yet.
Here's my guess at how this might work;
Two handlers;
PHP Code:
<?php
include_once('EventRegister.class.php');
// Kinda dodgy but how without instantiating FormHandler - better ideas?
$eventReg=& new EventSingleton;
$eventRegister=$eventReg->getReg();
$eventRegister->register('formResponse','FormHandler');
class FormHandler {
var $response;
function FormHandler {
switch ($_GET['formResponse']) {
case 'sendEmail':
$this->sendEmail();
break;
}
}
function sendEmail () {
// this could be invoking another class instead...
mail ( $_POST['to'],$_POST['from'],$_POST['subject'];
$this->response='Email sent';
}
function respond () {
echo ( $this->response );
}
}
?>
Then the default hander;
PHP Code:
<?php
// DefaultHandler.class.php
class DefaultHandler {
// Do default stuff here
function respond() {
// make whatever response
}
}
?>
Now a script that ties it all together;
PHP Code:
<?php
// email.php
include_once('FormHandler.class.php');
include_once('EventHandler.class.php');
include_once('EventListener.class.php');
$listener=& new EventListener;
$listener->trigger();
?>
That's the general idea at least...
Comments (including "You're soooo wrong"
) appreciated.
Still taking in pippo and AcidReigns comments. Certainly there could be some more efficient approach using inheritance and polymorphism. Wanted to avoid having to change existing classes though to provide "AddEvent" methods - perhaps that's where polymorphism can come in...
Bookmarks