Reputation: 14218
I am currently deciding if I should use events:
No events:
function receiveOrder($order) {
// ... do stuff
orderReceived($order);
}
function orderReceived($order) {
doSomeProcessing($order)
sendEmail($order)
}
function doSomeProcessing($order) {
}
function sendEmail($order) {
}
Events (this is no actual code, just the semantic structure)
function receiveOrder($order) {
// ... do stuff
event.emit('orderreceived', $order);
}
event.listen('orderreceived', doSomeProcessing);
event.listen('orderreceived', sendEmail);
function doSomeProcessing($order) {
}
function sendEmail($order) {
}
What is the advantage of using the events. I know that it is better if I want flexibility for others to use my package. This way they can hook into the receiveOrder function without modifying it. But assuming that I do not want to provide the code as a package (which is probably the case for most laravel projects). Why should I use events?
EDIT:
The reason why I don't like events is that it ruins the autocompletion and class resolution of IDEs and it is also much harder to debug/find side effects.
Upvotes: 0
Views: 68
Reputation: 2012
you are right,
Events allows you to separate application concerns and creates a mechanism to hook into actions in the application
Upvotes: 1