views:

74

answers:

1

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, and functions to just select one item from those lists. 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

Now, 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 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. 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. Also, the "local" functions all use the same function that performs the math, so should I group those together, or maybe just use inheritance... any suggestion???

While on the subject of oop, I have a user class that looks like... private $user = array();

public function Get_User_Data($uid){
  //get user data from database
  return $this->user;
}

public function Set_User_Data($user_array){
  $this->user = $user_array;
}

public function Add_User(){
  //INSERT data from $this->user into the database 
}

Now does anybody see anything wrong with the way this looks, mostly, should I set the user_data as a parameter for the Add_User function, instead of inserting the member variables?

+5  A: 

First of all you need to understand that taking your old procedural style functions and wrapping them inside of objects doesn't make your code object-oriented, it just means that you're writing more complicated and horribly ugly procedural code.

Secondly I would strongly recommend, in fact I cannot be too strenuous in my recommendations that you take some time to at least study the various PHP Frameworks that are out there. While you may never use any of them, I feel pretty safe in guaranteeing that studying any of them will give you a better grasp on object-oriented principles and good application design in general. In case you've never seen any before, the following should give a place to start:

  • Zend Framework
  • Symfony
  • CakePHP
  • Solar Framework

Additionally, if you've never heard of Martin Fowler or Patterns of Enterprise Application Architecture, I would strongly recommend that you try and pick up a copy. He literally wrote the book that has provided the basic patterns that are in use in EVERY popular web framework.

So much for my 'read the manual response' :-P

In your particular case I would start with a basic Active Record pattern to contain your database access logic and your domain logic. In this type of pattern, each database table (users, articles, comments) is represented by a discrete object. The basic Active Record class for Users would contain all of the functions to get a specific user or a list of users as well as the functions to insert, update, or delete a user. In addition, a User Active Record class would contain methods to load a user's articles and comments.

A shell User class might look something like this:

class User extends Active_Record {

public function find() {}

/**
 Single function performs inserts and updates for the object
**/
public function save() {}

public function delete() {}

public function getArticles() {}

public function getComments() {}
}
Noah Goodrich