WordPress.org

Ready to get started?Download WordPress

Forums

Issue with global variables (4 posts)

  1. Netsensei
    Member
    Posted 5 years ago #

    Hi,

    I was working on my plugin for Mollom (http://www.netsensei.nl/mollom) when I started noticing some odd behaviour when declaring certain variables global. For instance, If we create this simple plugin: (omitted the obligatory header for brevity's sake)

    function dosomething($do_comment) {
        global $do_comment; // let's declare it global
        print_r($do_comment);
        return $do_comment; // or alternatively: die();
    }
    add_action('preprocess_comment', 'dosomething');

    The $do_comment variable seems to be non-existant or empty! Meaning everything else breaks from here on as the input got lost somewhere along the way. But when we remove the 'global $do_comment' line, everything works like a charm and $do_comment passes the commentdata $correctly.

    Then again, why would you want to create extra global variables? Well, my plugin contains several different functions which are referenced on several occasions. Problem is mess with function signatures which make it easier for me to pass variables through globals.

    I know it's bad design and there are more gracious ways of passing stuff on. Like using OOP. But then again, I wanted my plugin just to work and pass data without getting it lost along the way.

    imho, this is just odd behaviour and I was curious if the list could give me some feedback or reasonable explanation on this one.

    With regards,

    Matthias Vandermaesen
    [sig moderated]

  2. Roger Theriault
    Member
    Posted 5 years ago #

    If you're just filtering the comment and the comment is being passed to the filter (action? which is it?), you shouldn't be declaring the function's parameter variable to be global. That's what's confusing me here... I can understand the need for globals, but I'd expect them to have a different name in your example.

    Now, on the topic of globals...

    There may be a do_comment global somewhere in WordPress. Or another plugin.

    But we all have to define functions anyway, and most of use are just doing it procedurally, so what's another global var?

    A graceful way to handle this is to only create one global for your plugin, and to make it somewhat unique so it is unlikely to collide with WordPress or another plugin.

    i.e. in your main plugin .php file:

    global $mollumvars;

    and then in your functions:

    mollum_set_comment($comment) {
        global $mollumvars;
        $mollumvars->comment = $comment;
    }
    mollum_dosomething($wp_comment) { // maybe a filter :-)
        global $mollumvars;
        $wp_comment = "<strong>" . $mollumvars->comment . "</strong>" . $wp_comment;
        return $wp_comment;
    }

    Anyone else care to chime in if they feel there's another way to do it?
    Or if I'm making no sense?

  3. Netsensei
    Member
    Posted 5 years ago #

    Thanks,

    Well, I tried to do it with several unique variables. I passed the data through the action through a $mollom_commentdata variable which I declared global. That's when things went wrong.

    As far as this goes, maybe I should use a singleton to store and transfer data across functions...

  4. Roger Theriault
    Member
    Posted 5 years ago #

    Well,

    function dosomething($do_comment) {
        global $do_comment; // let's declare it global

    If you pass a value in, you don't need to later declare it global scope. And because you already - at that line in your code - have a $do_comment, it gets taken out of scope by the global declaration. So after that line, your $do_comment does indeed have global scope but has not (apparently) been assigned a value. That's why I think you'll need two different variable names in your action. If you need to give whatever the function was passed a global scope so it can be accessed later, try this instead:

    function dosomething($passed) {
        global $global;
        $global = $passed;

    On top of that, WordPress may well be invoking code in an unexpected order, so your global has no value yet. It may be better to have your action calculate the necessary values or call the appropriate function in your plugin.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.