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
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
Comment