I know that the general advice to global variables is to not use them at all. Still, when doing something with the WordPress database, it is required to do global $wpdb;
to access the methods WordPress provides for interacting with the database.
Now I’ve written a small plugin that introduces several functions that need to interact with the WordPress database. That means, in every function I have written global $wpdb;
. After that, I build the table names needed dynamically: $wpdb->prefix . 'my_table_name';
.
This is a lot of redundant code in my opinion that even can produce a lot more work. For example, I changed the table names a few times during development and had to change it in every function of my plugin where I do $wpdb->prefix . 'my_table_name';
.
Now my question is: Is it a good idea to just global
$wpdb
outside of any function scope and then assign the tables names to global variables?
That way, everything would be a lot more flexible regarding table names (and maybe even performance?)