Global Data

I’ve seen this quite a few times, and it drives me nuts every time.

It’s very common for theme or plugin developers to setup options for their system in a large array and then make that array globally available. Ignoring whether developers should populate global variables or not, this is a perfectly fine method of gaining access to your data (but not always a good idea). However, your global variables should always be named in such a way as to NOT cause conflicts.

This evening I found this in a very popular commercial theme:

global $data;

$data? Really? This is a really, really bad idea because it is such a common variable name, which means that any other plugin or theme that decides to also follow this poor naming convention will cause a possibly dramatic conflict of global variables.

Advertisements

22 thoughts on “Global Data

  1. Not usually into embarrassment as a method of teaching, but please name and shame this time. Pretty please. With rum on top 🙂

      • *puts it on my shit list*
        (not that being sold on Theme Forest isn’t normally enough to get on my list 😉 )

      • There are a lot of truly fantastic themes on Theme Forest, though also a lot of themes that make it into category mentioned here.

        Putting a blanket “bad” label on themes just because they are on Theme Forest is extremely unfair to the authors that are doing really great work there.

  2. I was too harsh, apologies. Happy to here there are good themes on TF, it’s just I’ve never seen one (possibly, now I think about it, because people wouldn’t come to me wanting theme problems fixed or simple modifications done if their themes were well-coded – wonder why that didn’t occur to me before? Thanks!)

  3. I think it is easy to forget that global variables really are global, if coding too quick this is an easy mistake to make. A variable prefix and a more descriptive name is definitely needed.

  4. $data is default variable for SMOF, a popular theme options framework and that’s why it is a popular global variable which doesn’t matter since it’s a bad practice as you stated in your post.

    We just went with it and never changed it but we’re always updating and improving Avada. We do general updates and refactor our code whenever we get a chance.

    It is unfair to list Avada or any theme into a shit list.

    Thanks for the help!

    • If you acknowledge that it is bad, there is no reason or excuse to not change it. Just because you use an options framework does not mean you can ignore problems created by it. As a developer of an extremely widely used theme, it is your responsibility to ensure the code you write and the libraries you include are up to par.

      • That is definitely my goal and like I said whenever I get a chance, I refactor the code while developing for new features or working on bug fixes.

        It is not your job to inform me but if you find anything else within Avada, feel free to post it on your site and also inform me (i’ve subscribed to it as well).

        Thanks!

      • Actually someone else opinion does help a lot. Please contact me (you have my email) for probably a paid review of Avada for finding quirky code like this. It’ll be a major help!

      • Doh! It’s been having trouble all day (of course while I’m traveling). Use pippin(at)pippinsplugins(dot)com

  5. $data has been recently changed $smof_data -https://github.com/sy4mil/Options-Framework/issues/198

    Also see some of my replies there:

    “I agree that using global $data could potentially open to conflicts, but any plugins that uses $data on global scope is already a crappy plugin by itself.”

    SMOF was created way too long ago. Heck I don’t even remember the last time I made an update to it. It’s been on auto-pilot for quite some time now. I bet you can find a few dozens more crappy codes in there but the $data itself was inherited from the original Options Framework. Honestly, I don’t care.

    And you, yeah you, you’re on my shit list.

    • Great to hear it’s been updated. No one is getting judged for having written less-than-good code in past; every single developer (period) has done it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s