Page 1 of 1
Class structures
Posted: Mon Apr 18, 2005 4:24 am
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.
Posted: Mon Apr 18, 2005 7:24 am
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.
Posted: Mon Apr 18, 2005 10:06 am
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?
Posted: Mon Apr 18, 2005 10:15 am
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.
Posted: Mon Apr 18, 2005 11:08 am
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???