I'm struggling with the best way to best way to pass around a database 
handle to storage classes, and I'm not sure what the best way to do it is. 

Let's say I have a class like this (grossly oversimplified one) -- 
ideally I could call these as static methods, but it's not a requirement:

class Users {

    public function SetUser(User $user) {

          // Inserts a user into the database
          $sql = "Some SQL Goes Here";

          $result = $DB->query($sql);
          $DB::ifError($result) {
             die(DB::getError($result));
          }
        return true;
 }

What's the best way to make my database object ($DB) available to the 
methods?

I could setup the connection in the constructor, but I have storage 
classes for several data objects, many of which get instantiated 
simultaneously.  I don't want to take the performance hit of making seven

I could setup the connection in a global config file, and pass it as an 
argument to the constructor. 
This is what I've been doing, but it adds a lot of "bootstrapping" work 
to the client code, and prevents me from just doing User::set($user); in 
the client code, which would be so much nicer.

class User {
   
    private $DB;

    function  _constructor(DB $DB) {
          $this->DB = $DB;
    }

    public function SetUser(User $user) {

          // Inserts a user into the database
          $sql = "Some SQL Goes Here";

          $result = $this->DB->query($sql);
          $this->DB::ifError($result) {
             die($this->DB::getError($result));
          }
        return true;
    }
}

I could make the handle a superglobal, but I'm afraid that it limits my 
options, should I need to swap it out for a connection pool later. 
Maybe it doesn't -- just wondering if anyone has a good solution.

Thanks,
Jeromie


The php_mysql group is dedicated to learn more about the PHP/MySQL web database 
possibilities through group learning.  
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php_mysql/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to