[wp-hackers] General question on wpdb and classes

Andy Skelton skeltoac at gmail.com
Thu Aug 20 19:33:57 UTC 2009


You should either:
1. use the reference assign operator (&=) to ensure all of your
queries go to the same object instance, or
2. do "global $wpdb;" at the top of each method that uses $wpdb.

My preference is #2 because that how core always does it.

Andy

On Thu, Aug 20, 2009 at 1:53 PM, Christian
Gundersson<gundersson at gmail.com> wrote:
> Hi,
>
> 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.
>
> Thanks!
>
> Regards,
> Christian
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list