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
– Maniero
It’s a good question, already reformulated. It’s important for interested community to have a feedback on this topic.
– Calebe Oliveira
I don’t question the content. A simple change has already improved the shape.
– Maniero