Events on PHP. Is it possible?

后端 未结 6 667
悲&欢浪女
悲&欢浪女 2021-01-06 04:27

I\'m coming from the C# world and has just started doing a little PHP coding, so I was wondering if it is possible to use events on PHP, or if it is planned to include this

相关标签:
6条回答
  • 2021-01-06 05:05

    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.

    0 讨论(0)
  • 2021-01-06 05:06

    Stubbles has a pretty nice Event Dispatcher.

    0 讨论(0)
  • 2021-01-06 05:12

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

    0 讨论(0)
  • 2021-01-06 05:18

    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 .

    0 讨论(0)
  • 2021-01-06 05:19

    I would suggest Prado as well.

    0 讨论(0)
  • 2021-01-06 05:23

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

    0 讨论(0)
提交回复
热议问题