[wp-hackers] General question on wpdb and classes

Christian Gundersson gundersson at gmail.com
Thu Aug 20 18:53:14 UTC 2009


I'm writing a plugin and thought I'd use some OO programming with this to
make everything easy to maintain and extend. What I'm wondering is whether
I'm using $wpdb correctly. I usually let my objects fetch their own data
from the database instead of pre-fetching it and passing it in as an array
or what not. So, my classes need access to $wpdb and I do this currently
simply by adding

global $wpdb;
$this->_wpdb = $wpdb;

in the constructor of the class, the methods then use $this->_wpdb for
querying. This works as intended but I get the sense that this may not be
the correct/best way to do this.

What I'm wondering is first of all if I'm stupid and there's some apparent
danger in doing this, and secondly how you guys are handling this? Any tips,
hints or pointers in the general direction are welcome.



More information about the wp-hackers mailing list