Simple extension of PEAR DB
| From: | Peter Brodersen | Date: | Fri, 14 Jan 2005 11:24:08 +0000 |
| Subject: | Simple extension of PEAR DB | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-16934@lists.php.net to get a copy of this message | ||
Hi,
I'm using PEAR DB for most of my database work.
Though, I would like to extend the class to be able to use my own
application specific methods ("Fetch one page", "Fetch a specific
user", etc.).
I could create an ordinary function such as:
function getuser ($db, $id) {
return $db->getone("SELECT name FROM users WHERE id = ?",$id);
}
It works fine, but I don't like the idea of passing the object as an
argument. It would create a strange mix of using generic functions and
object-specific methods. Besides, the getuser()-function is closely
related to the usage of PEAR DB and would be better off as a method.
But I can't get an extended class to work. Code such as:
class MyProject_DB extends DB {
function getuser($id) {
return $mydb->getone("SELECT name FROM web_people WHERE id =
?",$id);
}
}
.. doesn't work:
It seems like there is a chicken-and-the-egg-problem here. DB_common
only gets read when DB::Connect has been called (and loaded e.g.
DB/mysql.php that loads DB/common.php ). So I can't extend DB_common
instead.
Is there a simple solution I have missed, or is the problem the
structure of PEAR DB?
Another possibility is to create a wrapper that connects as part of
the constructor, but I don't think this is the right approach for just
adding a simple method.
--
- Peter Brodersen