Class structures

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Class structures

Post by icarpenter »

Hi has anyone got any advice on how implement a good class structure? for example, would you put your SQL in one class all HTML in another and all arguments in another or would you generally have a class per page?

My current class 'new_page'...I only have one! is getting to around 300 lines. I would like to split out some of the functions but it would great if anyone has any ideas on how to do this?

Many Thanks Ian.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's a large discussion about most of that in the Theory board.

Basically, a class should do one thing. A function should only do one too, but at a smaller scale. Most pages would end up being around 20-30 objects, easily. All of which should be reusable from page to page.
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

Thanks Fayed...I am still none the wiser though when you say a class should do one thing only do you mean like 1 SQL select statment for example, or a menu function in HTML? or like a group of functions accociated with a particular page or area of site?
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

Post by Calimero »

My practise:

1 class - does one GROUP of closely connected functions
1 function - if you have 1 group of functions that share a same goal, then you just divide that into smaller chunks that make the functionality easier to implement is some other parts of the website or any application.


In your ex.:
1 Class: All SQL queries (execution) and maybe ( in my oppinion ) results retrieval for further use.
Functions in it are one Query and results for that query ( off course if you have several functionaly inter-connected queries - put them in one function.


Hope I made it a bit clearer how to use classes.

However - if I'm wrong those who are more expirienced then me are welcome to correct me.
User avatar
icarpenter
Forum Commoner
Posts: 84
Joined: Mon Mar 07, 2005 8:12 am
Location: Kent, England

Post by icarpenter »

it's getting a little clearer...so breaking down the SQL into smaller chunks ie:

Code: Select all

Class SQL()
{
var $results;

function query_table($x,$y) // amends table
  {
  $query = "UPDATE table SET field='$x' WHERE value='$y'";
  $this->query($query);
  }
function query($x)
  {				 
  $result = mysql_query($x)
      or die ("Couldn't execute query.");
  $this->results = mysql_fetch_array($result)	
  }
}
Would you then maybe have another class extended to the SQL class to echo the results???
Post Reply