Separate collection class or static methods in thmain class?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
whizzopia
Forum Newbie
Posts: 8
Joined: Sun Sep 02, 2007 6:17 pm

Separate collection class or static methods in thmain class?

Post 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]
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
whizzopia
Forum Newbie
Posts: 8
Joined: Sun Sep 02, 2007 6:17 pm

Post 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?
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.)
Post Reply