Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[resolved] global scope in plugins
Erm,
call me noob but I’m on the limit of my php/txp/plugin knowledge. Have I overseen something? Is there any mechanics inside TXP that prevend an global access on own variables inside of a plugin?
E.G.
$foo = 'bar';
function yab_print_foo()
{
global $foo;
return $foo;
}
All packed in a plugin … and it won’t work. :(
Digital nomad, sailing the world on a sailboat: 32fthome.com
Offline
Re: [resolved] global scope in plugins
Try this instead:
global $foo;
$foo = 'bar';
function yab_print_foo()
{
global $foo;
return $foo;
}
You need to explicitly declare $foo as a global variable, because the plugin code is executed in an eval construct from within a function.
Last edited by ruud (2009-07-25 17:18:24)
Offline
Re: [resolved] global scope in plugins
Won’t work too. :(
This gives me a syntax error.
Digital nomad, sailing the world on a sailboat: 32fthome.com
Offline
Re: [resolved] global scope in plugins
Ah yes, it will work:
$GLOBALS['foo'] = 'bar';
function yab_print_foo()
{
global $foo;
return $foo;
}
Digital nomad, sailing the world on a sailboat: 32fthome.com
Offline
Re: [resolved] global scope in plugins
I’ve edited my code example. Should work now. I mistakenly assumed that I could initialize a variable at the same time as declaring it global (similar to static variables).
Offline
Re: [resolved] global scope in plugins
Thanks Ruud,
your edited code works too.:)
Digital nomad, sailing the world on a sailboat: 32fthome.com
Offline