WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 2 years ago

#10471 closed feature request (maybelater)

Phase out the use of the global keyword

Reported by: prettyboymp Owned by:
Milestone: Priority: normal
Severity: trivial Version: 2.9
Component: General Keywords: global back-compat
Focuses: Cc:

Description

I'd like to request phasing out the use of the global keyword in exchange for get current instance methods. There are a couple reasons for this:

  1. Help reduce development errors by reducing the risk of variable name conflicts. I've often run into issues where I will name a local variable something before calling a core function that uses a global variable named the same thing.
  1. Clean up duplicate code. Lines like the below would only need to exist in the get method.
    global $wp_styles;
    if ( !is_a($wp_styles, 'WP_Styles') ) $wp_styles = new WP_Styles();
    

When doing this, the super globals would still need to be available for backwards compatibility.

Change History (3)

comment:1 dd325 years ago

  • Component changed from Canonical to General
  • Keywords back-compat added
  • Milestone changed from Unassigned to Future Release
  • Owner markjaquith deleted
  • Type changed from task (blessed) to feature request
  • Version set to 2.9

I dont think this'll be done anytime soon honestly.

You are right about variable conflicts, and its annoying as hell sometimes, but many themes and plugins also rely upon the global vars being available at this stage, many of the API functions/filters do not pass enough information right now for it to be avoided.

In order to this, its going to require a lot of back-compat breaking and a lot of filter/action re-working (mainly adding args). This can basically be thrown on the pile of items which would be great in core, we just need a release where we can break a lot of back-compat with.

IMO some vars should stay, $post and the like for example, but as many vars which are unexpected should go, along with vars which are actually objects (rewrite, query, etc)

comment:2 prettyboymp5 years ago

I don't know that even $post should still be accessed as a global variable. Since it is just pointing to the current post within the $wp_query posts array. Shouldn't it be like:

$wp_query &= WP_Query::get_instance(); or &= wp_get_wp_query_instance();
$post = $wp_query->get_the_query();

//and if php4 is finally dropped
$post = WP_Query::get_instance()->get_the_query();

comment:3 ryan2 years ago

  • Milestone Future Release deleted
  • Resolution set to maybelater
  • Status changed from new to closed

We've been massaging things in this direction. Eventually we'll get there. Let's take it case by case.

Note: See TracTickets for help on using tickets.