Hmm - in the category of "stuff I wished I knew before" - but that's what you get when you can only learn this stuff from books and the web.
I think I can pry a lot of code out of these global included variables files, and either create individual include files for each class or get them inside the classes themselves.
No, I don't suppose my classes would be truly portable, but the problem would be the same if the global variables were inside the classes - then the classes themselves would all need editing if the code moved to another site. In my setup, only three files of variables (common, public and admin) need editing.
Here's an example: I'm building a page sidebar, which is a list of categories, each with a list of items in it. The sql queries to retrieve the categories, and the items within each, could easily be included in each class. Specifics about the exact database name and table name to query could still be global I guess, or perhaps passed to the sidebar object as a function parameter.
When I start pulling out the results and formatting them, though, I want the actual html formatting to live outside the class (don't I?). So in my included (included in the page, not the class) variables file I have:
$sidebar_content_line = "<p class=\"sidebarcontent\"><a href=\"<!--LINKBASE-->?
id=<!--ID-->&catid=<!--CATID-->\" class=\"whitelink\"><!--TITLE--></a></p>\n";
$itemtype_to_url = array('news'=>'news.php','volop'=>'volunteer.php'...etc...);
And in my class I do something roughly like this:
function ParmsToTemplate($string) {
foreach ($this->parameters as $key => $value) {
$template_name = '<!--' . $key . '-->'
$string = str_replace($template_name, $value, $string);
}
return $string;
}
// and for each row in the sql result...
$this->parameters['LINKBASE'] = $GLOBALS['itemtype_to_url'][$row['itemtype']];
$this->parameters['ID'] = $row['id'];
$this->parameters['CATID'] = $row['catid'];
$this->parameters['TITLE'] = $row['title'];
$sidebar_formatte .= $this->ParmsToTemplate($GLOBALS['sidebar_content_line']);
In this case I only have a few formatting specifics - a couple of CSS classes - but other areas of the site have table cells with width data, background color, etc.
Seems to me it makes sense to have these very specific formatted lines live outside the classes, as line templates (with placeholders for their actual content). But maybe there's a better way to get them in than referencing $GLOBALS.
If you want to compartmentalise your code, consider using classes to encapsulate related pieces of data into their own objects (along with the methods used for manipulating them), and then passing those objects as parameters to the functions that need them.
I think I understand this. Basically you're saying group the variables that apply to a particular class into their own class, then create an instance of that class and pass it to the class that needs the variables. That seems to make sense. Not so sure about the "methods used for manipulating them" - those are already in the existing class.
How bad would it be to pass a single class with, say, 200 variables? Rather than break up my variables files, I could just pass them whole.
I'll have to read up on ini files and this single array bit. Any good links besides the manual?
Re. constants vs. variables - yes, good idea for ensuring they don't change (though I'm not doing any pass-by-reference really, except when instantiating classes and acting on them). Any other benefits to constants? Less overhead?
Thanks - I know this is long but I feel like I'm skiing with my legs crossed. I've learned a lot of 'correct' techniques but I'm basically teaching myself how to put them all together, and what 'works' is not necessarily the best way to do it.