PHP event mechanism based on AS3-PHP source code

Source: Internet
Author: User
PHP event mechanism based on AS3 PHP event mechanism based on AS3

 Type = trim ($ type);}/*** get event string */public function _ toString () {return $ this-> type ;}} /*** Event dispatch *** @ 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 ;} /*** dispatch Event ** @ param event $ Event * @ param string $ method * @ param string | object $ class * @ return void */public functi On 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 $ me Thod * @ 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 ;}} /*** 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, "meth Od "=> $ 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 ;} /*** check 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_Event (get_class ($ class ). "not exist ". $ method. "method", 1) ;}}/*** check whether the 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 );}} /*** check 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-> 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 );


The above is the PHP event mechanism implemented by the AS3 imitation. For more information, see PHP Chinese website (www.php1.cn )!

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.