Events on PHP. Is it possible?

元气小坏坏 提交于 2019-11-30 22:13:33

No event like C# in PHP but you can implement a Observer Pattern to attach delegate to be notified.

The Prado PHP Framework is an event driven framework that may appeal to you, especially since you are coming from the land of C# and presumably ASP.NET.

Take a look at the Quick Start. Specifically, take a look at the code under the Control Reference. There are plenty of code samples for you to look at and see if it is something like what you are looking for.

Stubbles has a pretty nice Event Dispatcher.

SPL - the Standard PHP Library provides the SplObserver and SplSubject interfaces for implementing the observer pattern in PHP

I would suggest Prado as well.

You can create something like event handling class in PHP :

class Event {
    protected $_eventCallbacks = array();
    function addEventCallback($callback) {
        $this->_eventCallbacks[$callback] = $callback;
    }
    function removeEventCallback($callback){
        if(isset($this->_eventCallbacks[$callback])){
            unset ($this->_eventCallbacks[$callback]);
        }
    }
    function cleanEventCallback(){
        foreach ($this->_eventCallbacks as $callback) {
            unset ($callback);
        }
    }
    function fireEvent() {
        foreach ($this->_eventCallbacks as $callback) {
            call_user_func($callback);
        }
    }
}

This code was taken from here http://setahost.com/php-events-singletone-and-factory-pattern-application/ There is also good example of modular and sub modular application using this class .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!