Discussion:
How to use $wpdb in plugins
Leo Baiano
2014-01-06 15:58:50 UTC
Permalink
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.

How do you do?
--
Amplexos,

Leo Baiano
Web Developer
Stephen Harris
2014-01-06 16:03:32 UTC
Permalink
If you are referring to the default database tables then there are
functions available that allow you to add/remove/insert records. If
these are custom tables then I typically would create functions which
handle those operations, along with any required validation and
sanitisation. That way any SQL statements (and the global $wpdb) are
contained within those three (or thereabouts) functions.
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
Eric Andrew Lewis
2014-01-06 16:35:43 UTC
Permalink
I think the best practice here is to define and use the $wpdb global in every method you need it. You could perhaps store it by reference in a class variable, or create a singleton wrapper for accessing $wpdb, but both of these are stepping out of the “WordPress” way, and add a level of abstraction that isn’t adding much benefit, and perhaps confusing other programmers that touch your code.

--
Eric Andrew Lewis
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Leo Baiano
2014-01-06 18:14:58 UTC
Permalink
I'm currently using calling global methods at all, I was not sure if that
was the best practice and why I sent this email.
Post by Eric Andrew Lewis
I think the best practice here is to define and use the $wpdb global in
every method you need it. You could perhaps store it by reference in a
class variable, or create a singleton wrapper for accessing $wpdb, but both
of these are stepping out of the “WordPress” way, and add a level of
abstraction that isn’t adding much benefit, and perhaps confusing other
programmers that touch your code.
--
Eric Andrew Lewis
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm
working
Post by Leo Baiano
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,

Leo Baiano
Web Developer
Ben May
2014-01-06 22:53:08 UTC
Permalink
I usually have a base class that all other classes extend, and have a
self::db() which basically globals $wpdb once and returns it, saves having
to write global $wpdb; each class.
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Leo Baiano
2014-01-07 00:32:46 UTC
Permalink
I liked the idea, I'll do a test here.
Post by Ben May
I usually have a base class that all other classes extend, and have a
self::db() which basically globals $wpdb once and returns it, saves having
to write global $wpdb; each class.
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm
working
Post by Leo Baiano
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,

Leo Baiano
Web Developer
Guus (IFS)
2014-01-07 09:09:32 UTC
Permalink
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.

-----Original Message-----
From: Ben May
Sent: Tuesday, January 07, 2014 6:53 AM
To: wp-***@lists.automattic.com
Subject: Re: [wp-hackers] How to use $wpdb in plugins

I usually have a base class that all other classes extend, and have a
self::db() which basically globals $wpdb once and returns it, saves having
to write global $wpdb; each class.
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Chris Williams
2014-01-07 14:30:16 UTC
Permalink
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
Gabriel Acosta
2014-01-07 14:38:19 UTC
Permalink
Static calls are discouraged? will be disallowed? o my god this list does
gets hilarious from time to time. What is actually discouraged, not from
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Leo Baiano
2014-01-07 14:41:30 UTC
Permalink
I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Post by Gabriel Acosta
Static calls are discouraged? will be disallowed? o my god this list does
gets hilarious from time to time. What is actually discouraged, not from
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,

Leo Baiano
Web Developer
Guus (IFS)
2014-01-07 15:03:46 UTC
Permalink
PHP 5.4 and higher or something gives notices on static calls.

-----Original Message-----
From: Leo Baiano
Sent: Tuesday, January 07, 2014 10:41 PM
To: wp-***@lists.automattic.com
Subject: Re: [wp-hackers] How to use $wpdb in plugins

I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Post by Gabriel Acosta
Static calls are discouraged? will be disallowed? o my god this list does
gets hilarious from time to time. What is actually discouraged, not from
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,

Leo Baiano
Web Developer
J.D. Grimes
2014-01-07 15:06:32 UTC
Permalink
Yes, but only if the method isn’t declared static.
Post by Guus (IFS)
PHP 5.4 and higher or something gives notices on static calls.
I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Post by Gabriel Acosta
Static calls are discouraged? will be disallowed? o my god this list does
gets hilarious from time to time. What is actually discouraged, not from
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Leo Baiano
2014-01-07 15:15:36 UTC
Permalink
Not if you declare the method as static.
Post by Guus (IFS)
PHP 5.4 and higher or something gives notices on static calls.
-----Original Message----- From: Leo Baiano Sent: Tuesday, January 07,
[wp-hackers] How to use $wpdb in plugins
I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Static calls are discouraged? will be disallowed? o my god this list does
Post by Gabriel Acosta
gets hilarious from time to time. What is actually discouraged, not from
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,

Leo Baiano
Web Developer
Gabriel Acosta
2014-01-07 15:52:37 UTC
Permalink
"and higher or something" yup, sounds legit, lol
Post by Leo Baiano
Not if you declare the method as static.
Post by Guus (IFS)
PHP 5.4 and higher or something gives notices on static calls.
-----Original Message----- From: Leo Baiano Sent: Tuesday, January 07,
[wp-hackers] How to use $wpdb in plugins
I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Static calls are discouraged? will be disallowed? o my god this list
does
Post by Guus (IFS)
Post by Gabriel Acosta
gets hilarious from time to time. What is actually discouraged, not from
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Guus (IFS)
2014-01-07 16:44:48 UTC
Permalink
Hehehehe.

It is legit though :).

-----Original Message-----
From: Gabriel Acosta
Sent: Tuesday, January 07, 2014 11:52 PM
To: wp-***@lists.automattic.com
Subject: Re: [wp-hackers] How to use $wpdb in plugins

"and higher or something" yup, sounds legit, lol
Post by Leo Baiano
Not if you declare the method as static.
Post by Guus (IFS)
PHP 5.4 and higher or something gives notices on static calls.
-----Original Message----- From: Leo Baiano Sent: Tuesday, January 07,
[wp-hackers] How to use $wpdb in plugins
I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Static calls are discouraged? will be disallowed? o my god this list
does
Post by Guus (IFS)
Post by Gabriel Acosta
gets hilarious from time to time. What is actually discouraged, not from
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Gabriel Acosta
2014-01-07 16:58:10 UTC
Permalink
It's not:
http://www.php.net/manual/en/language.oop5.static.php

Calling non-static methods statically generates an *E_STRICT* level warning.

That is completely different than "PHP 5.4 and higher or something gives
notices on static calls."
Post by Guus (IFS)
Hehehehe.
It is legit though :).
-----Original Message----- From: Gabriel Acosta
Sent: Tuesday, January 07, 2014 11:52 PM
Subject: Re: [wp-hackers] How to use $wpdb in plugins
"and higher or something" yup, sounds legit, lol
Not if you declare the method as static.
Post by Leo Baiano
Post by Guus (IFS)
PHP 5.4 and higher or something gives notices on static calls.
-----Original Message----- From: Leo Baiano Sent: Tuesday, January 07,
[wp-hackers] How to use $wpdb in plugins
I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Static calls are discouraged? will be disallowed? o my god this list
does
Post by Guus (IFS)
gets hilarious from time to time. What is actually discouraged, not >>
from
Post by Guus (IFS)
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in future versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Chris Williams
2014-01-07 17:20:51 UTC
Permalink
And completely different from "discouraged" and/or "disallowed".

Static methods are a common OOP technology, it makes no sense to
"discourage" let alone "disallow" them.

<sheesh>
Post by Gabriel Acosta
http://www.php.net/manual/en/language.oop5.static.php
Calling non-static methods statically generates an *E_STRICT* level warning.
That is completely different than "PHP 5.4 and higher or something gives
notices on static calls."
Guus (IFS)
2014-01-16 06:16:37 UTC
Permalink
Thanks, didn't know that the warning could be bypassed with the static
declaration.

--------------------------------------------------
From: "Gabriel Acosta" <***@gmail.com>
Sent: Wednesday, January 08, 2014 12:58 AM
To: <wp-***@lists.automattic.com>
Subject: Re: [wp-hackers] How to use $wpdb in plugins
Post by Gabriel Acosta
http://www.php.net/manual/en/language.oop5.static.php
Calling non-static methods statically generates an *E_STRICT* level warning.
That is completely different than "PHP 5.4 and higher or something gives
notices on static calls."
On Tue, Jan 7, 2014 at 9:44 AM, Guus (IFS)
Post by Guus (IFS)
Hehehehe.
It is legit though :).
-----Original Message----- From: Gabriel Acosta
Sent: Tuesday, January 07, 2014 11:52 PM
Subject: Re: [wp-hackers] How to use $wpdb in plugins
"and higher or something" yup, sounds legit, lol
Not if you declare the method as static.
Post by Leo Baiano
Post by Guus (IFS)
PHP 5.4 and higher or something gives notices on static calls.
-----Original Message----- From: Leo Baiano Sent: Tuesday, January 07,
[wp-hackers] How to use $wpdb in plugins
I believe you are mistaken Guus, it makes little sense to discourage the
use of Static calls
Static calls are discouraged? will be disallowed? o my god this list
does
Post by Guus (IFS)
Post by Gabriel Acosta
gets hilarious from time to time. What is actually discouraged, not
from
Post by Guus (IFS)
Post by Gabriel Acosta
the PHP standard but from other methodologies like OOP is the use of
globals, but clearly that's just the way WP is so...
Post by Chris Williams
Evidence please?
Post by Guus (IFS)
In newer PHP versions static calls are discouraged. Guess in
future
versions
even disallowed.
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Dobri
2014-01-07 14:43:12 UTC
Permalink
How is

$db = self::db();
//do stuff with $db

different from

global $wpdb;
//do stuff with $wpdb

Unless you use it for one call only (self::db()->query() or something), I don't see much benefit in this method?

~Dobri
Post by Ben May
I usually have a base class that all other classes extend, and have a
self::db() which basically globals $wpdb once and returns it, saves having
to write global $wpdb; each class.
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Guus (IFS)
2014-01-07 15:02:40 UTC
Permalink
LOL

I had a similar idea already :).

-----Original Message-----
From: Dobri
Sent: Tuesday, January 07, 2014 10:43 PM
To: wp-***@lists.automattic.com
Subject: Re: [wp-hackers] How to use $wpdb in plugins

How is

$db = self::db();
//do stuff with $db

different from

global $wpdb;
//do stuff with $wpdb

Unless you use it for one call only (self::db()->query() or something), I
don't see much benefit in this method?

~Dobri
Post by Ben May
I usually have a base class that all other classes extend, and have a
self::db() which basically globals $wpdb once and returns it, saves having
to write global $wpdb; each class.
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Abdussamad Abdurrazzaq
2014-01-08 12:26:48 UTC
Permalink
Can't you stick it in a class variable instead?
Post by Dobri
How is
$db = self::db();
//do stuff with $db
different from
global $wpdb;
//do stuff with $wpdb
Unless you use it for one call only (self::db()->query() or something), I don't see much benefit in this method?
~Dobri
Post by Ben May
I usually have a base class that all other classes extend, and have a
self::db() which basically globals $wpdb once and returns it, saves having
to write global $wpdb; each class.
Post by Leo Baiano
What better way to use the global $ wpdb in building a plugin? I'm working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Nikola Nikolov
2014-01-08 12:33:17 UTC
Permalink
I still think that adding

global $wpdb;

is probably the best approach. If you really don't want to copy/paste a
single line of code, you can just use $GLOBALS['wpdb'] - same thing, just a
bit less readable(and you won't be able to use the nifty features of things
like code intel).

Anyway - whichever way you choose it usually won't be a big deal if you
decide to change your implementation in the future - you'd have to do just
a couple of search/replace's.


On Wed, Jan 8, 2014 at 2:26 PM, Abdussamad Abdurrazzaq <
Post by Abdussamad Abdurrazzaq
Can't you stick it in a class variable instead?
Post by Dobri
How is
$db = self::db();
//do stuff with $db
different from
global $wpdb;
//do stuff with $wpdb
Unless you use it for one call only (self::db()->query() or something), I
don't see much benefit in this method?
~Dobri
I usually have a base class that all other classes extend, and have a
Post by Ben May
self::db() which basically globals $wpdb once and returns it, saves having
to write global $wpdb; each class.
What better way to use the global $ wpdb in building a plugin? I'm
Post by Leo Baiano
working
on a plugin and some methods need to access data in the database tables,
including creating, and I was in doubt about the best way, like, do not
want to quit repeating global $ wpdb in all methods of my class.
How do you do?
--
Amplexos,
Leo Baiano
Web Developer
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
_______________________________________________
wp-hackers mailing list
http://lists.automattic.com/mailman/listinfo/wp-hackers
Loading...