[wp-trac] [WordPress Trac] #39795: WordPress objects are only accessible via global variables
WordPress Trac
noreply at wordpress.org
Mon Feb 6 16:29:04 UTC 2017
#39795: WordPress objects are only accessible via global variables
-------------------------+------------------------------
Reporter: jdmweb | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: trunk
Severity: normal | Resolution:
Keywords: | Focuses:
-------------------------+------------------------------
Comment (by jdmweb):
Replying to [comment:2 swissspidy]:
> Even if we'd add `get_wpdb()` or `get_wp_filesystem()`, those functions
would still use global variables.
You're absolutely right, but I'd say that's not what makes them
interesting, but rather the fact that they could serve as point of
exchange between the core and plugins, allowing plugins to get rid of
global uses if they want. And furthermore, centralizing access to
WordPress main objects behind a commun function could also be an
opportunity to change the code that is executed behind this function
without having to rewrite any plugin code in the future.
Let's say for now you keep the code you'd write in a plugin:
In WordPress core:
{{{
function get_wpdb(){
global $wpdb;
return $wpdb;
}
}}}
And in your plugin:
`$wpdb = get_wpdb();`
Then in the future, you decide to change this to use dependency injection
or any other way discussed in the very interesting tickets you mentioned,
(even though I must admit that after 456 comments it gets a bit difficult
to keep an idea of where we're at), you'd implement this behind the
get_wpdb() function .
What do you think of this first baby step?
I also was thinking that sometimes, in case of $wpdb for example, the code
is rather simple to get access to the object. But some other times, for
$wp_filesystem for example, you can't just use the global variable, you
also have to check if the object exists, if the class is loaded, if not
load the class, which can result in about 10 lines of codes here and
there. And as it's something I don't like todo, I abstract the code in
functions already.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/39795#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list