PHP event Mechanism (2) _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
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 string| | Object $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 string| | Object $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;
}
}
/**
* Detects if 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 the specified class has a specified method
* @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);
}
}
/**
* Detects if the 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 if a listener function exists for the specified event
* @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 an array of listener functions
* @param string $eventstr
* @param string $function
* @return Array
*/
Private Function _create_listener_fn ($EVENTSTR, $function) {
Return Array (
"Object" =>false,
"Function" = $function,
);
}
/**
* Create an array of listener classes
* @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 ();
}

http://www.bkjia.com/PHPjc/323047.html www.bkjia.com true http://www.bkjia.com/PHPjc/323047.html techarticle Copy the code as follows: PHP class event extends stdclass{public $target =null, public $type =null;/** * Create event * @param string $type */Public Function __construct ($type) ...

  • 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.