By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

PHP MySQL Class use

P: n/a
Okay, so I'm looking at making my pages use one connection (link) for
multiple queries (as it should be done). What I've done so far is
created a class for the database access which queries can be run
through, which is straight forward enough, but I'm wondering what would
be the cleanest and most efficient way of using it.

I've got multiple classes which need the database connection at any
point during the page, sometimes for more than one method. Should I pass
the database object when the other objects are constructed, making it a
class variable? E.g.

$object = new Object($db); // pass the mysql object here, store it to be
// used as $this->db in the object when a query
// is needed.

Or should I pass it on a method-by-method basis? E.g.

$object->requestDetails($db); // with $db being the mysql object
$object->deleteDetails($db); // second use, same db object

I realise that it'll end up working either way, but is there a standard
on the way this should be done? I don't want to start coding the project
and then rewrite it half way through because I don't like the way it works.

Thanks for the thoughts.

~ sock
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In my infrastructure I have a separate class for each business entity
(database table) but each of these class instances communicate to the
database through a single DML class instance. The trick is to create the DML
instance in a global variable where it can be accessed by any and all
business entity instances.

I have put together a sample application (described in
http://www.tonymarston.net/php-mysql...plication.html) which can be
run online. You can also download all the source code to see how it is done.

Hope this helps.

--
Tony Marston
http://www.tonymarston.net

"erotic sock" <er****@sock.com> wrote in message
news:40**********************@news.optusnet.com.au ...
Okay, so I'm looking at making my pages use one connection (link) for
multiple queries (as it should be done). What I've done so far is
created a class for the database access which queries can be run
through, which is straight forward enough, but I'm wondering what would
be the cleanest and most efficient way of using it.

I've got multiple classes which need the database connection at any
point during the page, sometimes for more than one method. Should I pass
the database object when the other objects are constructed, making it a
class variable? E.g.

$object = new Object($db); // pass the mysql object here, store it to be
// used as $this->db in the object when a query
// is needed.

Or should I pass it on a method-by-method basis? E.g.

$object->requestDetails($db); // with $db being the mysql object
$object->deleteDetails($db); // second use, same db object

I realise that it'll end up working either way, but is there a standard
on the way this should be done? I don't want to start coding the project
and then rewrite it half way through because I don't like the way it works.
Thanks for the thoughts.

~ sock

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.