WordPress.org

Ready to get started?Download WordPress

Ideas

Global functions for global variables.

  1. anadgouda
    Member

    I understand that global variables are convenient for extending WordPress. However, it is also dangerous since someone can inadvertently write into the global variable, affecting the entire system. Instead of providing access to the variables directly, can WordPress provide a set of global functions that return the variable? For example


    getDB()->query();

    instead of


    $wpdb->query();

    This will not enforce but allow global variables to read from, without using the explicit global variable.

    If someone wants to write into them, he/she can choose to use the explicit global variable.

    Posted: 7 years ago #
  2. John Blackbourn
    Member

    12345

    I don't see how this will prevent people from inadvertently writing to the global variable, as the global variable must still exist in order to be written to when needed.

    If someone writes their own plugin and is worried about inadvertently writing to the global variable, they can create their own function within the plugin to give themselves this functionality.

    Posted: 7 years ago #
  3. zackdesign
    Member

    Global variables have been killing us when creating a certain plugin. Must be fixed! Template tags like the_title and the_content were crapping themselves out :(

    And this was even when we were following recommended loop guidelines!

    Posted: 6 years ago #
  4. scribu
    Member

    Because using getter functions can't be enforced, it won't solve much.

    Until WP moves to PHP5-only syntax, this will remain a problem.

    Suggest closing.

    Posted: 4 years ago #
  5. The globals are there for easy access to the functionality both from within plugins and within themes.

    However the data is stored within the running script it has to exist in something within the global space.

    Adding functions to get hold of things like the current instance of the db class don't add any benefit and just end up increasing code bloat.

    Posted: 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    12345
    81 Votes
  • Status

    Sorry, not right now