[nycphp-talk] Accessing a method from one class in another
Chris Weyl
cweyl at alumni.drew.edu
Mon Jan 16 21:26:37 EST 2006
On 1/16/06, CED <Consult at covenantedesign.com> wrote:
> You create an object for DB called for when accessing objects that need that
> particular connection... Ahh AOP seems so applicable here. =D
When I only need a single db connection (e.g. the db/userid/passwd are
the same for all acceses), I actually tend to just add to my base
class (from which all other classes derive at some level:
class Base {
...
public static $dbh = NULL;
....
public static function get_dbh() {
if (self::$dbh == NULL)
self::$dbh = //connection code here//
return self::$dbh;
}
}
That way, I only ever have to open the connection to the database
once, and can keep on reusing the same object no matter where I am by
calling Base::get_dbh(). You get the idea.
Come to think of it, this wouldn't even need to be in your base class,
if such a thing doesn't work nicely for you; you could stick it in a
standalone class of its own, and just access the static methods.
-Chris
More information about the talk
mailing list