At 14:24 04.03.2003, Joseph Szobody spoke out and said: --------------------[snip]-------------------- >I have several web projects that are all database driven. I have recently >been diving into OOP, and rewriting a lot of procedural code in OOP. I have >a design question about handling the MySQL connection. > >I have a mysql() class which handles all my queries, automatic >inserts/updates, etc. Just about every other class will use the mysql() >class at some point. > >Should I.... > >1) Make every class extend mysql(), so that they all have direct access to >the db functions
I wouldn't do that. First of all this would counterfeit the OOP goodies of encapsulating the actual implementation, i.e. your MySQL class wouldn't be the "transparent blöackbox" it ought to be. >2) Create a new mysql object inside of each class. That's a better way but you need to manage the database connection to avoid repeated reconnects... only way if to make it "class static". >3) Only create the mysql object at the script level, and pass in the object >to the other classes. This would be my choice and is exactly what I'm doing, not only for database objects but for all other objects I'm using. This is my approach: I have a centralized object storage. No global variables, only a couple of public functions. For example, the function (note: not a method) "pos_getobject" returns a reference (!) to any object. If it doesn't exist it will be created. The syntax is: $hObj =& pos_getobject($id); where "$id" is the object ID. This ID could be either a constant (e.g. OBJID_DATABASE), or an ID referencing a database-persistent object. In my case, object ID's that are negative numbers are predefined objects (e.g. OBJID_DATABASE), positive numbers are DB persistent objects (simply the row-ID where the object header can be retrieved), and non-numeric IDs are temporary objects that may be created at runtime. If you don't need to take such a general approach I'd create a public function to retrieve the database object: $hDB =& pos_getDB($dbid); where $dbid is a database identifier in case you need it. Just my 2c, -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php