akbkhome

Notes on signal ideas for PHP

Signals / Events in PHP


There are 3 projects I know of that could do with a more native way to implement Events
  • debug callback on dbdo
  • observer in PEAR_Exception (and eventually Warning)
  • php-gtk

C# has a relatively new way of implementing this (while not perfect, it is interesting)

in abbreviated C# syntax this is what it looks like.

class xxxx {

     virtual method_prototype(x,y);
     public event eventvar method_prototype;

public somemethod() {
       eventvar += new Delegate(mycallback);
eventvar(); // initiate the signals.
   }
public mycallback(x,y) {
printf("eventvar fired");
}
}

In more natural PHP code, this cold be:


class xxxx {
    // event registers a method, that hooks into the generic event handler.
     public event $onDebug = array();


    function somemethod() {

// add a handler to an event..
// this is the natrual syntax for this, but does open the door to someone wiping the stack!
       $this->onDebug[] = array($this,'mycallback');


// initiate the signals.
$this->onDebug($x,$y);
   }
// a handler..
function mycallback($x,$y) {
echo "debug hander got $x , $y\n";
}
}

What would be involved in implementing this?

  • event  recognize by tokenizer/parser (a string) ? do we have typehints for object args? -that would help..
  • when parsing, register a method that  points to a generic signal emitor)
  • the signal emitor just tests all the elements in the variable, and trys to call them...

Example implementation in PHP: (eg. what it would replaced if it was done at the engine level)


    
class myClass {
/* This is the bit that is implemented by the sytnax "public static event $onDebug;" */
static var $onDebug = array();
static function onDebug() {
foreach(myClass::$onDebug as &$call) {
// handler returns true to block other events.
if (call_user_func_args($call, func_get_args())) {
return;
}
}
}


function doSomething() {
self::onDebug("calling doSomething");
}
}

function mydebugger($str) {
echo "DEBUG: $str\n";
}
// register an event handler
myClass::$onDebug[] = 'mydebugger';


$x = new myClass();
$x->doSomething(); // prints DEBUG: calling doSomething









Add a comment (requires javascript!)

Name
Email
Homepage
Comment
 

Edit Document | Create Page:
Contact me at alan@akbkhome.com