Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the big picture difference between observers and callbacks?

What's the big picture difference between observers and callbacks? When and where to use which one?

like image 675
Mohit Jain Avatar asked May 17 '10 15:05

Mohit Jain


1 Answers

It's about separation of concerns.

Observers allow you to factor out code that doesn't really belong in models. For example, a User model might have a callback that sends a registration confirmation email after the user record is saved, but you don't really want this code in the model because it's not directly related to the model's purpose.

Observers allow you to have that clean separation because you don't have all that callback code in your models. Observers depend a model (or models), not the other way around.

like image 145
John Topley Avatar answered Sep 28 '22 07:09

John Topley