Page 1 of 1

Separate collection class or static methods in thmain class?

Posted: Sun Sep 02, 2007 7:14 pm
by whizzopia
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, I'm new to oop and I have a question about it.

Let's say that I have a 'Student' class with a few functions:

Code: Select all

class Student{
 public function addStudent();
 public function deleteStudent();
}
Now if I need a function 'getAllStudents()' that return a list of all students,
where should I put that function?
Should I put it inside 'Student' class? or should I create a new class for it?

Thanks


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Sep 02, 2007 7:19 pm
by Ollie Saunders
Please use a more descriptive thread title in future.
Should I put it inside 'Student' class?
Yes :D
You should call it getAll() because you already know it's referring to students from the name of the class, same applies for the other methods.

Posted: Sun Sep 02, 2007 8:52 pm
by whizzopia
Thanks for your reply

But I have one more question :D
The 'Student' class has properties such as Student Id, first name, last name,etc.
The 'getAll()' method is only used to get a list of all students. It doesn't need to modify/access the class properties.

So, should I create 'getAll' method as a static method?

Posted: Sun Sep 02, 2007 10:07 pm
by Z3RO21
whizzopia wrote:Thanks for your reply

But I have one more question :D
The 'Student' class has properties such as Student Id, first name, last name,etc.
The 'getAll()' method is only used to get a list of all students. It doesn't need to modify/access the class properties.

So, should I create 'getAll' method as a static method?
Have your student class contain information about the individual student (name, age, ect..) then have a class called studentsClass (something similar) that contains all of the students of that class. If you are doing what I think you are doing by storing the information in a database look into object relational mapping. Hope this helps

Posted: Mon Sep 03, 2007 1:39 am
by Weirdan
So, should I create 'getAll' method as a static method?
Either that or create a separate finder class for students. Using Propel convention it would be named StudentsPeer (with methods such as findAll, findByName, findByAge, etc.)