Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - is it okay for business models to know each other?

Simple question: in MVC, is it okay for one model to interact with another? For example, with an Auth model...can it interact with the User model - or is there a better way to go about it?

Should the middle man be the controller?

like image 864
johnnietheblack Avatar asked Dec 23 '22 06:12

johnnietheblack


2 Answers

Different Model classes interacting is just fine. Most complex objects aren't completely standalone. As with any good OO code, the classes should use public methods and not expose their implementation to each other, but beyond that, it's all good.

like image 115
dj_segfault Avatar answered Dec 28 '22 07:12

dj_segfault


They can certainly know about each other, and utilize each other's functions. Often, best practices will keep this one-way, but not always. Like dj_segfault said, this interaction should be through public methods. Be sure to read up on Dependency injection; changing the internal workings of one class should not break the other, as long as all public methods still behave the same.

like image 24
keithjgrant Avatar answered Dec 28 '22 08:12

keithjgrant