Copy Code code as follows:
<?php
/**
* Event exception
*
* @author Lonely
* @create 2010-10-21
* @version 0.1
* @lastupdate Lonely
* @package Event
*/
Class Exception_event extends Exception {}
/**
* Event Object
*
* @author Lonely
* @create 2010-10-21
* @version 0.1
* @lastupdate Lonely
* @package Event
*/
Class Event extends stdclass{
public $target =null;
Public $type =null;
/**
* Create Event
* @param string $type
*/
Public function __construct ($type) {
$this->type=trim ($type);
}
/**
* Get Event String
*/
Public Function __tostring () {
return $this->type;
}
}
/**
* Event Distribution
*
* @author Lonely
* @create 2010-10-21
* @version 0.1
* @lastupdate Lonely
* @package Event
*/
Class eventdispatcher{
Private $_callback_method;
/**
* Add Event
* @param Event $event
* @param string $method
* @param string| | Object $class
* @return Boolean True
*/
Public function Attach (Event $event, $method, $class =null) {
$event->target= $this;
$eventstr = $this->_create_event_str ($event);
if ($this->has ($event, $method, $class))
return true;
if ($class!=null) {
$this->_check_method ($class, $method);
$this->_callback_method[$eventstr][]= $this->_create_listener_method ($eventstr, $class, $method);
}else{
$this->_check_function ($method);
$this->_callback_method[$eventstr][]= $this->_create_listener_fn ($eventstr, $method);
}
return true;
}
/**
* Distribution of events
* @param Event $event
* @param string $method
* @param string| | Object $class
* @return void
*/
Public Function Dispatch (Event $event) {
$eventstr = $this->_create_event_str ($event);
if ($this->_check_callback ($eventstr)) {
foreach ($this->_callback_method[$eventstr] as $v) {
if ($v [' object ']) {
if (Is_object ($v [' class '])) {
$v [' class ']-> $v [' method '] ($event);
}else{
Call_user_func (Array ($v [' class '], $v [' method ']), $event);
}
}else{
$v [' function '] ($event);
}
}
}
}
/**
* Delete Event
* @param Event $event
* @param string $method
* @param string $class
* @return Boolean True
*/
Public Function Detact (Event $event, $method, $class =null) {
$eventstr = $this->_create_event_str ($event);
if (! $this->_check_callback ($EVENTSTR))
return true;
if (! $this->has ($event, $method, $class))
return true;
if ($class!=null) {
$this->_check_method ($class, $method);
foreach ($this->_callback_method[$eventstr] as $k => $v) {
if ($v = = $this->_create_listener_method ($eventstr, $class, $method)) {
unset ($this->_callback_method[$eventstr] [$k]);
return true;
}
}
return true;
}else{
$this->_check_function ($method);
foreach ($this->_callback_method[$eventstr] as $k => $v) {
if ($v = = $this->_create_listener_fn ($eventstr, $method)) {
unset ($this->_callback_method[$eventstr] [$k]);
return true;
}
}
return true;
}
}
/**
* Detect whether the event is listening
* @param Event $event
* @param string $method
* @param string $class
* @return Boolean
*/
Public function has (Event $event, $method, $class =null) {
$eventstr = $this->_create_event_str ($event);
if (($class!=null)) {
$this->_check_method ($class, $method);
if ($this->_check_callback ($eventstr)) {
foreach ($this->_callback_method[$eventstr] as $v) {
if (Is_object ($v [' class '])) {
$v _class=get_class ($v [' class ']);
}else{
$v _class= $v [' class '];
}
if (Is_object ($class)) {
$s _class=get_class ($class);
}else{
$s _class= $class;
}
$temp _v=array (
"Class" => $v _class,
"Method" => $method,
);
$temp _s=array (
"Class" => $s _class,
"Method" => $method,
);
if ($temp _v== $temp _s) {
return true;
}
}
}
}else{
$this->_check_function ($method);
if ($this->_check_callback ($eventstr)) {
foreach ($this->_callback_method[$eventstr] as $v) {
if ($method = = $v [' function ']) {
return true;
}
}
}
}
return false;
}
/**
* Detects whether a specified method exists for a given class
* @param string $class
* @param string $method
* @exception exception_event
* @return void
*/
Private Function _check_method ($class, $method) {
if (!method_exists ($class, $method)) {
throw new Exception_event (Get_class ($class). "Not exist". $method. "Method", 1);
}
}
/**
* Detect whether a specified function exists
* @param string $function
* @return void
*/
Private Function _check_function ($function) {
if (!function_exists ($function)) {
throw new Exception_event ($function. "Function not exist", 2);
}
}
/**
* Detects whether a specified event has a listener function
* @param string $eventstr
* @return Boolean
*/
Private Function _check_callback ($EVENTSTR) {
if (Isset ($this->_callback_method[$eventstr])
&&is_array ($this->_callback_method[$eventstr])
){
return true;
}
return false;
}
/**
* Create a listener function array
* @param string $eventstr
* @param string $function
* @return Array
*/
Private Function _create_listener_fn ($EVENTSTR, $function) {
Return Array (
"Object" =>false,
"Function" => $function,
);
}
/**
* Create a Listener class array
* @param string $eventstr
* @param string $class
* @param string $method
* @return Array
*/
Private Function _create_listener_method ($eventstr, $class, $method) {
Return Array (
"Object" =>true,
"Class" => $class,
"Method" => $method,
);
}
/**
* Create Event string
* @param Event $event
* @return String
*/
Private Function _create_event_str (event $event) {
$classstr =strtolower (Get_class ($event));
$eventstr = (string) $event;
return $CLASSSTR. $eventstr;
}
}
Class Test extends eventdispatcher{
}
function T ($e) {
Print_r ($e->a);
}
$v =new test ();
$e =new Event ("test");
$v->attach ($e, "T");
$v->detact ($e, "T");
echo $v->has ($e, "T");
$e->a= "DD";
$v->dispatch ($e);