php事件系统实现 [英] php event system implementation

查看:151
本文介绍了php事件系统实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的自定义MVC框架中实现一个事件系统,以允许解耦
需要彼此交互的类。基本上,任何类别触发一个事件的能力和任何其他类可以侦听这个事件能够挂钩的能力。



然而,我似乎找不到一个正确的实现给出了php的共享的性质没有架构。



例如,假设我有一个User模型,每次更新它,它触发一个userUpdate事件。现在,此事件对于A类(例如)有用,因为它需要在用户更新时应用自己的逻辑。
但是,当用户更新时,A类未加载,因此无法绑定到User对象触发的任何事件。



这样一个场景?
我错了吗?



任何想法都将不胜感激

解决方案

在触发事件之前,必须有A类的实例,因为必须注册该事件。一个例外是如果你注册一个静态方法。



假设你有一个用户类应该触发一个事件。首先你需要一个(抽象的)事件调度器类。这种事件系统的作品类似于ActionScript3:

 抽象类Dispatcher 
{
protected $ _listeners = array ();

public function addEventListener($ type,callable $ listener)
{
// fill $ _listeners array
$ this-> _listeners [$ type] [] = $ listener;
}

public function dispatchEvent(Event $ event)
{
//调用所有侦听器并将事件发送到callable的
if($ this - > hasEventListener($ event-> getType())){
$ listeners = $ this-> _listeners [$ event-> getType()];
foreach($ listeners as $ callable){
call_user_func($ callable,$ event);
}
}
}

public function hasEventListener($ type)
{
return(isset($ this-> _listeners [ $型]));
}
}

您的用户类现在可以扩展Dispatcher: p>

  class User extends Dispatcher 
{
function update()
{
//做你的更新逻辑

//触发事件
$ this-> dispatchEvent(new Event('User_update'));
}
}

如何注册该事件?说你有一个方法 update

  //非静态方法
$ classA = new A();
$ user = new User();
$ user-> addEventListener('User_update',array($ classA,'update'));

//方法更新为静态
$ user = new User();
$ user-> addEventListener('User_update',array('A','update'));

如果您有正确的自动加载,可以调用静态方法。
在这两种情况下,事件将作为参数发送到更新方法。如果你喜欢,也可以有一个抽象的Event类。


I would like to implement an Event system in my custom MVC framework, to allow decoupling of classes that need to interact with each other. Basically, the ability for any class to trigger an event and any other class that listens for this event to be able to hook into it.

However, I cannot seem to find a correct implementation given the nature of php's share nothing architecture.

For instance, let's say that I have a User model that each time that it is updated, it triggers a userUpdate event. Now, this event is useful for class A (for instance) as it needs to apply its own logic when a user is updated. However, class A is not loaded when a user is updated, so it cannot bind to any events triggered by the User object.

How can you get around such a scenario? Am I approaching it wrongly?

Any ideas would be greatly appreciated

解决方案

There must be an instance of class A before the event is triggered because you must register for that event. An exception would be if you'd register a static method.

Let's say you have an User class which should trigger an event. First you need an (abstract) event dispatcher class. This kind of event system works like ActionScript3:

abstract class Dispatcher
{
    protected $_listeners = array();

    public function addEventListener($type, callable $listener)
    {
        // fill $_listeners array
        $this->_listeners[$type][] = $listener;
    }

    public function dispatchEvent(Event $event)
    {
        // call all listeners and send the event to the callable's
        if ($this->hasEventListener($event->getType())) {
            $listeners = $this->_listeners[$event->getType()];
            foreach ($listeners as $callable) {
                call_user_func($callable, $event);
            }
        }
    }

    public function hasEventListener($type)
    {
        return (isset($this->_listeners[$type]));
    }
}

Your User class can now extend that Dispatcher:

class User extends Dispatcher
{
    function update()
    {
        // do your update logic

        // trigger the event
        $this->dispatchEvent(new Event('User_update'));
    }
}

And how to register for that event? Say you have class A with method update.

// non static method
$classA = new A();
$user = new User();
$user->addEventListener('User_update', array($classA, 'update'));

// the method update is static
$user = new User();
$user->addEventListener('User_update', array('A', 'update'));

If you have proper autoloading the static method can be called. In both cases the Event will be send as parameter to the update method. If you like you can have an abstract Event class, too.

这篇关于php事件系统实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆