I am working on a new project and am curious as to how I should go about doing something properly.
Lets say I have a table called “leads” in my database. This clearly means that a single “lead” would be an object, and I’d need a class in order to create, update, or pull information for said “lead” object.
Now here is where my question comes in. Lets say I have a page called leads.php with a table that I want to populate with multiple “leads” from my database. Do I create the method for listing all leads within the “lead” class itself, or do I create a seperate “data service” class, which would represent a service object used to retrieve multiple leads?
Any help would be greatly appreciated.
3
If you add a method Lead.list()
where it returns a list of leads it’s violation of SRP, as the fetching a list of Leads is not the responsibility of Lead
object.
So the best option is to use a data service or a repository. For example LeadRepository
could have methods GetAll(), GetById(id)