Sunday, 1 September 2013

Global scope not being set

Global scope not being set

I'm working on a plugin for MyBB and for some reason, it seems PHP is not
setting global scope to some of my variables.
I have the following code in my plugin file:
$mod_template = 'member_login';
$search_string = '{$footer}';
$replace_string = 'hello';
function myplugin_activate()
{
global $mod_template, $search_string, $replace_string;
$data = "_activate()\n";
$data .= '$local_var = '.$local_var."\n";
$data .= '$mod_template = '.$mod_template."\n";
$data .= '$search_string = '.$search_string."\n";
$data .= '$replace_string = '.$replace_string."\n";
$data .= 'line1: #'.preg_quote($search_string).'#i'."\n";
$data .= 'line2: #'.preg_quote($replace_string).'#i';
file_put_contents('/tmp/log1.txt', $data);
}
function myplugin_deactivate()
{
global $mod_template, $search_string, $replace_string;
$data = "_deactivate()\n";
$data .= '$local_var = '.$local_var."\n";
$data .= '$mod_template = '.$mod_template."\n";
$data .= '$search_string = '.$search_string."\n";
$data .= '$replace_string = '.$replace_string."\n";
$data .= 'line1: #'.preg_quote($search_string).'#i'."\n";
$data .= 'line2: #'.preg_quote($replace_string).'#i';
file_put_contents('/tmp/log2.txt', $data);
}
The code is the same for the body of both functions (except of course for
the name of the file and the title placed inside the file).
When I activate and deactivate my plugin, the two log files are generated
and I get the following:
log1.txt
_activate()
$local_var = testing
$mod_template = member_login
$search_string = {$footer}
$replace_string = hello
line1: #\{\$footer\}#i
line2: #hello#i
log2.txt
_deactivate()
$local_var = testing
$mod_template =
$search_string =
$replace_string =
line1: ##i
line2: ##i
So, in the myplugin_deactivate() function, the global scope is not being
set for some reason.

No comments:

Post a Comment