Code: Select all
Class Events{
static private $Events = array();
public function fireEvent($name){
if(isset(self::$Events[$name])){
foreach(self::$Events[$name] as $method){
$method(&$this);
//call_user_func($method, &$this);
}
return true;
}
return false;
}
public function addEvent($name, $Fname){
if(!empty($Fname)){
self::$Events[$name][] = $Fname;
return true;
}
return false;
}
public static function listEvents(){
print '<pre>'; print_r (self::$Events); print '</pre>';
}
}
// Each Class that wants Events would have to Extend the class
Class Test extends Events{
function __construct(){
$this->Var = 'test';
$this->fireEvent('Onload');
}
function say(){
$this->fireEvent('preSay');
echo $this->Var;
$this->fireEvent('Say');
}
}
// Fire Functions
function Test_load($Obj){
print 'Loaded Object:';
print '<pre>';
print_r ($Obj);
print '</pre>';
print '<hr>';
}
function Test_preSay($Obj){
$Obj->Var = 'Welcome!';
print 'Set Var as Welcome';
print '<hr>';
}
function Test_Say1($Obj){
print '<br>Finished Say';
print '<hr>';
}
function Test_Say2($Obj){
print 'Say 2!';
print '<hr>';
}
// Obj::addEvent( Event Name , Function Name );
Test::addEvent('Onload','Test_load');
Test::addEvent('preSay','Test_preSay');
Test::addEvent('Say','Test_Say1');
$Test = new Test();
// Works Also But Not for Onload
$Test->addEvent('Say','Test_Say2');
$Test->say();
$Test->listEvents();
/*
Javscript does it like so..
Test.addEvent('Say', function(){
// Code
// and you can use `this` (self Object)
});
*/Code: Select all
Loaded Object:<pre>Test Object
(
[Var] => test
)
</pre><hr>Set Var as Welcome<hr>Welcome!<br>Finished Say<hr>Say 2!<hr><pre>Array
(
[Onload] => Array
(
[0] => Test_load
)
[preSay] => Array
(
[0] => Test_preSay
)
[Say] => Array
(
[0] => Test_Say1
[1] => Test_Say2
)
)
</pre>Or Reference Each Variable In an Array, and then Pass the Array also with the Object as another Arg.
I did have the Referenced Array working, but it just seemed like to much dirty work to get protected/private variables, but then you still wouldn't have the functions.
Any input on this, is it just pointless? I could use some Evals, but i don't want to sacrifice speed.
It could be just me when i messed around with javascript alot for a couple weeks.