What are the right practices to handle events in PHP?

Asked

Viewed 716 times

7

I created some classes and an interface to manipulate events for a small MVC application. My intention is to implement the Observer standard, but I do not know if it agrees, since I still confuse myself with it in PHP. In the following code, I did it the right way, or it may cause some maintenance problem in the future?

<?php
# Interface do listener 
interface ActionListener {
    public function actionPerformed(ActionEvent $e);
}

# O evento
class ActionEvent {

    private $eventSource;
    private $id;
    private $command;
    private $time;

    public function __construct($eventSource, $id, $command = '') {
        $this->eventSource = $eventSource;
        $this->id = $id;
        $this->command = $command;
        $this->time = time();
    }

    public function getEventSource() {
        return $this->eventSource;
    }

    public function getID() {
        return $this->id;
    }

    public function getCommand() {
        return $this->command;
    }

    public function getTime() {
        return $this->time;
    }
}

# Uma fonte de eventos.
class EventSource {

    private $listeners = [];

    public function notify($id, $command = '') {
        $event = new ActionEvent($this, $id, $command);
        foreach ($this->listeners as $actual) {
            $actual->actionPerformed($event);
        }
    }

    public function addActionListener(ActionListener $object) {
        $this->listeners[] = $object;
    }
}
  • You have not been clear in your questioning. You need to say exactly what you need. A Q&A website is not suitable for asking for opinions. The platform works well for objective questions. Explicitly, we should not ask for opinions but rather solutions that can be answered relatively briefly. I can see more of a reason to close this question the way it is formulated. I’ll wait if you can get it objective. It seems to me a question of Code Review. See in English ways to order CR: http://codereview.stackexchange.com/help/on-topic

  • It’s a good question, already reformulated. It’s important for interested community to have a feedback on this topic.

  • I don’t question the content. A simple change has already improved the shape.

1 answer

2


The Design Pattern Observer (also known as Publish-Subscribe) is actually a kind of one-to-many relationship between objects, i.e., when an object changes its state, the objects dependent are updated.

Event-oriented programming is in some ways a somewhat more abstract concept, and can be implemented in several ways.

Generally, event-based technologies tend to be asynchronous, for example the Reactphp.

But assuming the question is about implementing Design Pattern Observer, there is no official implementation of any standard, so that article can be a good starting point.

Normally, for the implementation of this standard, we have the classes Splobserver and Splsubject of the PHP Standard Library (SPL) which are interfaces for you to implement the standard.

I recommend looking at such interfaces and comparing with their standard implementation, and if possible, tailoring their classes to implement such interfaces.

In this article in Portuguese, you can find an example of implementing the standard using the SPL interfaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.