The examples in this article describe how PHP implements the event mechanism. Share to everyone for your reference. as follows:
<?php/** * Event/class Event {Private $callbacks = array ();
Private $holder;
function __construct () {$BT = Debug_backtrace ();
if (count ($BT) < 2) {$this->holder = null;
Return
} $this->holder = & $BT [1][' object '];
function Attach () {$args = Func_get_args ();
Switch (count ($args)) {Case 1:if (is_callable ($args [0])) {$this->callbacks[]= $args [0];
Return
} break; Case 2:if (is_object ($args [0]) && is_string ($args [1])) {$this->callbacks[]= array (& $args [0], $ar
GS[1]);
} return;
Default:return;
}} function Notify () {$BT = Debug_backtrace ();
if ($this->holder && ((count ($BT) >= 2 && $BT [Count ($BT)-1][' object ']!== $this->holder) || (Count ($BT) < 2)))
{Throw (New Exception (' Notify can only is called in Holder '));
foreach ($this->callbacks as $callback) {$args = Func_get_args (); Call_user_func_array ($callbacK, $args);
}
}
}
I hope this article will help you with your PHP programming.