[wp-hackers] odd behaviour vs. global variables
Matthias Vandermaesen
matthias.vandermaesen at gmail.com
Thu Jun 12 12:30:54 GMT 2008
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
--
http://www.netsensei.nl
More information about the wp-hackers
mailing list