PHP event Mechanism (2)

Source: Internet
Author: User
Tags foreach array
Copy CodeThe code is as follows:
<?php
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;
}
}

Abstract class eventdispatcher{
Private $_callback_method;
/**
* Add Event
* @param Event $event
* @param string $method
* @param stringobject $class
* @return Boolean True
*/
Public function Attach ($event, $method, $class =null) {
$eventstr = (string) $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 stringobject $class
* @return void
*/
Public Function Dispatch (Event $event) {
$event->target= $this;
$eventstr = (string) $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, $method, $class =null) {
$eventstr = (string) $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, $method, $class =null) {
$eventstr = (string) $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 (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 ($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,
);
}
}



Class MyEvent extends event{
Const aa= ' AAA ';
}
Class Ball extends eventdispatcher{
Public Function aa () {
$event =new MyEvent (MYEVENT::AA);
$this->dispatch ($event);
}
}

Class MyTest {
Public Function test () {
$ball =new Ball ();
$ball->attach (MYEVENT::AA, ' KK ');
$ball->aa ();
}
Public Function KK ($e) {
Print_r ($e);
}
}


try{
$t =new mytest ();
$t->test ();
}catch (Exception $e) {
echo $e->getmessage ();
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.