Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-07-25 16:01:26

trenc
Plugin Author
From: Malmö
Registered: 2008-02-27
Posts: 572
Website GitHub

[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. :(

Offline

#2 2009-07-25 16:21:06

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#3 2009-07-25 16:29:10

trenc
Plugin Author
From: Malmö
Registered: 2008-02-27
Posts: 572
Website GitHub

Re: [resolved] global scope in plugins

Won’t work too. :(

This gives me a syntax error.

Offline

#4 2009-07-25 16:59:53

trenc
Plugin Author
From: Malmö
Registered: 2008-02-27
Posts: 572
Website GitHub

Re: [resolved] global scope in plugins

Ah yes, it will work:

$GLOBALS['foo'] = 'bar';

function yab_print_foo()
{
        global $foo;
        return $foo;
}

Offline

#5 2009-07-25 17:19:18

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#6 2009-07-25 17:46:37

trenc
Plugin Author
From: Malmö
Registered: 2008-02-27
Posts: 572
Website GitHub

Re: [resolved] global scope in plugins

Thanks Ruud,

your edited code works too.:)

Offline

Board footer

Powered by FluxBB