I'm relatively new using OOP in PHP. It's helped immensely in the organization and maintenance of my code, but I'd like to get better at designing my classes and using OOP as efficiently as I can. I've read the Gang of Four Design Patterns book, but still need some help. After building a few small apps, here's one thing I keep running across.

Let's say I'm building an application that keeps track of enrollment information for a school.

The way I would currently approach this is to have a class called student, and methods within that class for CRUD on an individual student's record. It seems logical that I would have a constructor method for this class that took the student_id as an argument, so I could reference it from within the object for all of those different CRUD operations.

But then, as I continue building the app, I run across situations where I need to run queries that return multiple students. For instance, something like, get_all_students_from_grade($grade), get_dropdown_of_all_students(), etc. These methods don't apply to just one student, so it seems odd that I would have them as methods in my student class, since I instantiated the object with one student_id in mind. Obviously I can make it work this way, but it seems like I'm 'doing it wrong.' What is the best way to approach this?

Accepted Answer

Written by
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki