Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP design question

Tags:

oop

php

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?

like image 362
Matthew Avatar asked Jan 01 '10 18:01

Matthew


People also ask

What is object-oriented design question?

The object-oriented design (OOD) question is centered around writing out a program to model a real-world system. For example, one OOD question I've been asked was to design a restaurant. The solution would involve specifying the important classes and methods for managing a restaurant.

What is object-oriented design example?

For example, in a Library Automation Software, each library representative may be a separate object with its data and functions to operate on these data. The tasks defined for one purpose cannot refer or change data of other objects. Objects have their internal data which represent their state.


2 Answers

Separate the student class (which is a domain class) from the operations on it (the business logic or data access, depending on the case) like:

  • student - the domain object contains only data
  • student_service or student_dao (Data Access Object) - performs operations

This is sometimes considered as breaking the encapsulation, but it is an accepted best practice.

Here's more information on the matter. It provides more drawbacks from OOP point of view than the breaking of encapsulation. So even though it appears to be an accepted practice, it is not quite OOP.

like image 193
Bozho Avatar answered Sep 21 '22 12:09

Bozho


Break it into two classes:

  1. student
  2. student_repository

Your student class knows nothing about how it is stored relationally.

$students = student_repository.get_all_students_from_grade($grade)
like image 20
ChaosPandion Avatar answered Sep 19 '22 12:09

ChaosPandion