<?php
include 'objbox.class.php';
include 'objhandler.class.php';
// small debug helper function
function debug($var) { echo "<pre>".stripslashes(var_export($var,true))."</pre>"; }
// an example event handler function
function event_handler( event $ev ) {
debug($ev);
}
// set the evenet handler
objhandler::set_event_handler('event_handler');
// an example object to be passed around
class passobj {
public $var = 0;
}
// an axample object number 1
class example1 {
public function hook($obj) {
debug("example1");
$obj->var++;
}
public function hook_with_exception() {
throw new exception('some bad has happened.');
}
}
// an example object number 2
class example2 {
public function hook($obj) {
echo debug("example2");
$obj->var++;
}
public function hook_with_exception() {
// nothing bad happened here
//throw new exception('some bad has happened.');
}
}
// create the new container
$box = new objbox();
// add some objects
$box['e1'] = new example1();
$box[] = new example2();
debug($box);
$var = new passobj();
debug($var);
$box->hook($var); // var plus 2 | 2 Events are fired
debug($var);
// access directly like this
$box['e1']->hook($var); // var plus 1 | Event is fired
// or like this
$box->e1->hook($var); // var plus 1 | Event is fired
debug($var);
$box->hook_with_exception(); // 3 events are fired, to calls, and 1 exception
|