Forum Moderators: coopster

Message Too Old, No Replies

Good design pattern for my classes?

         

megaman732

5:55 am on Aug 27, 2009 (gmt 0)

10+ Year Member



I'm starting to work with oop to build a site of user generated data.

There are different types of data coming from my classes. I have functions that get a list of data from the database, functions to just select one item from those lists, functions to select a list based on distance... For example

function Get_Article($aid); //Gets an article
function Get_Users_Articles($uid); //Gets a multidemsional array of the users articles
function Get_Latest_Articles(); //Self explanatory by now
function Get_Local_Articles($zip); //Gets articles written by local users
function Get_Local_Users($zip); //Gets all local users

function Get_All_Article_Comments($aid); //Gets a multidimensional array of the comments written for an article
function Get_Article_Comment($cid); //Gets a single article comment

How should I set up my classes to organize these functions. Should I just put them all in the same class, or should I seperate them... Like seperate the comments from the articles, or maybe seperate the functions that retrieve a single article/comment from those that retrieve a list of articles/comments, or functions that select based on distance, since those require a certain function to get the distance. I might add more things to the site later that allow for comments, so I was thinking of just seperating all the comment functions from the others, or using inheritance... any suggestion?

Also, I have a User class and I was wondering...

Should I use member variables for something like this, or just pass parameters... for example.

class User{
private $user = array();

public function Get_Existing_User($user_id){
//SQL selects user info for existing user
$this->user = mysqli_fetch_assoc();
}

public function Set_User($user_data){
$this->user = (array) $user_data;
}

public function Add_User(){
//insert everything from $this->user into database
}

public function Get_User(){
return $this->user;
}
}
VS

class User{

public function Get_Existing_User($user_id){
//SQL selects user info for existing user
$user = mysqli_fetch_assoc();
return $user;
}

public function Add_User($user_data){
//insert everything from $user_data into database
}
}

Also, should I use an array like $user to hold all the user data since thats how its coming out of the database anyways, or should I just make a variable for everything like
$name, $email, etc...

Also, does anybody recommend using php's magic methods like __set, __get, etc...

I would appreciate it if somebody could give me a good solid design pattern and structure to use, and tell me why its good... Thanks alot in advance, I know I have a lot of questions...

dreamcatcher

8:55 am on Oct 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi megaman732,

There isn`t really any set ways of designing classes. Everyone has their own methods. As classes & functions work in many different ways, its hard to drill down to a set routine that would be the most common.

If you find a good book on OOP, that should help you out with class structure. As you further develop your coding skills, you`ll find your own preferences as you go along.

Good luck.

dc