PHP event mechanism implementation code using AS3 imitation

Source: Internet
Author: User
PHP event mechanism implementation code using AS3. For more information, see. The code is as follows:


/**
* 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 an event
* @ Param string $ type
*/
Public function _ construct ($ type ){
$ This-> type = trim ($ type );
}
/**
* Get the event string
*/
Public function _ toString (){
Return $ this-> type;
}
}
/**
* Event dispatching
*
* @ Author lonely
* @ Create 2010-10-21
* @ Version 0.1
* @ Lastupdate lonely
* @ Package Event
*/
Class EventDispatcher {
Private $ _ callback_method;
/**
* Add events
* @ 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;
}
/**
* Dispatch 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 an 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;
}
}
/**
* Check whether the event is listened
* @ 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;
}
/**
* Checks whether a specified method exists for a specified 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 );
}
}
/**
* Checks 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 );
}
}
/**
* Checks 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 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 an 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-> );
}
$ 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 );

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.