Okay, here's what I've got so far.
I like to keep things organized, so all my classes are divided into their seperate areas of expertise. I've got a session.class, mail, form, and database class.
I've implemented a singleton design pattern to the best of my ability, so every page does at least a couple specific things at this point. First, it includes the configuration file -- this holds all the site wide constants and variables, as well as another class, called 'ke' for the sake of this post which only has two static function calls. One of those being the singleton loader.
Now what I want to do, is organize even further as I get deeper into the depths of this beast. I want to split the database class up into child classes. Right now it opens the connection on __construct, has basic $object->query() type functions and then all the UAC and session based database calls. What I want is a very stripped down database class that only has the database connection and basic query type methods, and then I'll sort all the UAC methods into another class, all the session database methods into another, and eventually, database methods specific to their cause into their own classes.
Now here's my conundrum. It seems simple enough, add the parent class definition to my config file, which will be included on every page regardless, then load the child classes into memory as I need them and be on my merry way
But if for example, I call uac.database.class and gallery.database.class, and other.database.class on one page, am I not opening three seperate database connections with each call to parent::__construct()? What if I'm calling four other children on one page? 6? 10? I doubt it, but what if?
I'm thinking maybe I can reduce this using the singleton by simplifying my database class down to just the necessary calls, and making fully independent (non-child) classes for the UAC, gallery, et al which just get the database object themselves in each function call... or can I call my database class via __construct and make yet another reference to it?
PHP Code:
public function __construct() {
$this->db =& ke::getInstance('database');
}
public function doSomething() {
$q = "SELECT name FROM table WHERE this='that'";
return mysql_query($q, $this->db->connection);
}
I just thought of that right now... seems in my head like it should work. That won't cause any kind of circular program logic will it? Having getInstance() call a class who's construct calls getInstance()? Seems to me like it shouldn't be a problem, but I'm still naive on a few counts.
I think I'll give that a go, and see if the whole yard blows up in the process. Any thoughts though?
-mark