Home  >  Article  >  php教程  >  php实现事件监听与触发实例程序

php实现事件监听与触发实例程序

WBOY
WBOYOriginal
2016-05-25 16:40:211622browse

闲来无事,想了想PHP如何实现事件监听,参考了jQuery的事件绑定思路,简单的实现了一下.

主要功能:1.绑定事件 支持一个事件绑定多个动作,支持绑定一次性事件,2.触发事件,3.注销事件

php实现事件监听与触发实例程序,代码如下:

<?php
class Event  
{  
    protected static $listens = array();  
       
    public static function listen($event, $callback, $once=false){  
        if(!is_callable($callback)) return false;  
        self::$listens[$event][]    = array(&#39;callback&#39;=>$callback, &#39;once&#39;=>$once);  
        return true;  
    }  
       
    public static function one($event, $callback){  
        return self::listen($event, $callback, true);  
    }  
       
    public static function remove($event, $index=null){  
        if(is_null($index))  
            unset(self::$listens[$event]);  
        else 
            unset(self::$listens[$event][$index]);  
    }  
       
    public static function trigger(){  
        if(!func_num_args()) return;  
        $args                       = func_get_args();  
        $event                      = array_shift($args);  
        if(!isset(self::$listens[$event])) return false;  
        foreach((array) self::$listens[$event] as $index=>$listen){  
            $callback               = $listen[&#39;callback&#39;];  
            $listen[&#39;once&#39;] && self::remove($event, $index);  
            call_user_func_array($callback, $args);  
        }  
    }  
}
?>

以下是一些调用的例子: 

<?php
// 增加监听walk事件  
Event::listen(&#39;walk&#39;, function(){  
    echo "I am walking...n";  
});  
// 增加监听walk一次性事件  
Event::listen(&#39;walk&#39;, function(){  
    echo "I am listening...n";  
}, true);  
// 触发walk事件  
Event::trigger(&#39;walk&#39;);  
/*  
I am walking...  
I am listening...  
*/ 
Event::trigger(&#39;walk&#39;);  
/*  
I am walking...  
*/ 
   
Event::one(&#39;say&#39;, function($name=&#39;&#39;){  
    echo "I am {$name}n";  
});  
   
Event::trigger(&#39;say&#39;, &#39;deeka&#39;); // 输出 I am deeka  
Event::trigger(&#39;say&#39;, &#39;deeka&#39;); // not run  
   
class Foo  
{  
    public function bar(){  
        echo "Foo::bar() is calledn";  
    }  
       
    public function test(){  
        echo "Foo::foo() is called, agrs:".json_encode(func_get_args())."n";  
    }  
}  
   
$foo    = new Foo;  
   
Event::listen(&#39;bar&#39;, array($foo, &#39;bar&#39;));  
Event::trigger(&#39;bar&#39;);  
   
Event::listen(&#39;test&#39;, array($foo, &#39;test&#39;));  
Event::trigger(&#39;test&#39;, 1, 2, 3);  
   
class Bar  
{  
    public static function foo(){  
        echo "Bar::foo() is calledn";  
    }  
}  
   
Event::listen(&#39;bar1&#39;, array(&#39;Bar&#39;, &#39;foo&#39;));  
Event::trigger(&#39;bar1&#39;);  
Event::listen(&#39;bar2&#39;, &#39;Bar::foo&#39;);  
Event::trigger(&#39;bar2&#39;);  
 
function bar(){  
    echo "bar() is calledn";  
}  
   
Event::listen(&#39;bar3&#39;, &#39;bar&#39;);  
Event::trigger(&#39;bar3&#39;); 
?>


文章网址:

随意转载^^但请附上教程地址。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn