Re: Simple extension of PEAR DB

From: Date: Fri, 14 Jan 2005 17:29:16 +0000
Subject: Re: Simple extension of PEAR DB
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-16938@lists.php.net to get a copy of this message
Peter Brodersen wrote:
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?
No, you have analyzed the problem properly and your conclusion is right as well.
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.
No. You could simply use they global keyword or simply use the $GLOBALS superglobal. function getuser ($id) { global $db; return $db->getone("SELECT name FROM users WHERE id = ?",$id); } function getuser ($id) { return $GLOBALS['db']->getone("SELECT name FROM users WHERE id = ?",$id); } I strongly recommend the second version as global might fail you if you layer your scopes deeply. regards, Lukas

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