Re: Extending PEAR::DB (static constructor issue?)

From: Date: Fri, 10 Mar 2006 04:06:18 +0000
Subject: Re: Extending PEAR::DB (static constructor issue?)
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-23255@lists.php.net to get a copy of this message
On 3/9/06, weston@leary.csoft.net <weston@leary.csoft.net> wrote: > So... I'm trying to extend PEAR::DB. It'd be great to keep everything it offers and > just add a few more perhaps unconventional functions. > > Intuitively, it seemed like this approach might work: > > <?PHP > > require_once("DB.php"); > > # Toy Extension of DB Class > class DBToyExt extends DB > { > var $foo = 1; > var $bar = 2; > > function testext($x) > { > echo "\nHEY: $x"; > } > } > > $dte = DBToyExt::connect("mysql://weston_tssa:******@localhost/weston_tssa"); > > $dte->testext('testing'); # Error happens here > $dte->testext($dte->moo); > $dte->testext($dte->bar); > > ?> > > However, it doesn't seem to understand that the method "testext" exists, and > gives me a fatal error to that effect, as you can see: > > http://weston.canncentral.org/web_lab/mlib/DBToyExt.php > > I'm guessing this is a side effect of the static constructor -- apparently using the > "extends" keyword is enough to help the engine know that the class DBToyExt is supposed to > inherit the static function "connct", but it's not enough to bless the return value > of connect from the class DB to DBToyExt.... > > How do I get around this and extend PEAR::DB? > There is absolutely no way in PHP to know what class a statuc method is being called on (well, beside using debug_backtrace(), which would be a horrible solution), so DB does the normal thing and returns a normal DB object. However, if you'd actually looked into the connect method you'd know that you don't even get a DB back. You get a DB_* which is of the type of driver that you asked for. The right way to extend DB would be to extend a specific driver and use that in your DSN. For example, I had a oci8ext driver that I used to add some extra oci8 functions. This of course means that you're making changes specific to the driver, but there is no way to get around this with DB. -- Justin Patrin

« previous php.pear.general (#23255) next »