Issue with global variables
-
Hi,
I was working on my plugin for Mollom (https://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]
- The topic ‘Issue with global variables’ is closed to new replies.