Chris
Chris

Reputation: 14218

laravel function vs event for code that is only used by the owner

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

Answers (1)

HSLM
HSLM

Reputation: 2012

you are right,

Without Events, Lots of Logic in One Place

Events allows you to separate application concerns and creates a mechanism to hook into actions in the application

Upvotes: 1

Related Questions