Page 1 of 1

Problems using globals

Posted: Wed Sep 21, 2005 5:49 pm
by Glenn
Because I can't seem to get globals to work within functions, I've created the simplest function for testing:

Code: Select all

$num = "2";

function glenn() {

global $num;

echo	"Number: ".$num;

}

glenn();
If I save that as an include file (glenn.function.php) and go right to it with my browser I get:

Number: 2

But if I try to include that file into a template (require_once) or try creating a user defined tag and inserting it I get:

Number:

Of course using the function without the global works:

Code: Select all


function glenn() {

$num = "2";

echo	"Number: ".$num;

}

glenn();
The global doesn't work. So how do I get globals to work in CMSMS?

Thanks!

Re: Problems using globals

Posted: Wed Sep 21, 2005 8:01 pm
by sjg
I wrote a whole response, based on an incorrect assumption of what you're trying to do, then I realized I was way off. So here I try again :)

Note that the "functions" that live in the plugins directory are not arbitrary PHP functions, but need to use the function API to work!

I think you won't have much luck including directly into a template with require_once. I think using user-defined tags is a better bet. To pass data, I'd put my values into the cms global's "variables" array.

For example, if I create user tag "test" containing:

Code: Select all

global $gCms;
$vars = &$gCms->variables;
$vars['number'] = 4;
and another user tag "test2" containing:

Code: Select all

global $gCms;
echo $gCms->variables['number'];
and put {test}{test2} into my page or template, it will replace those two tags with a "4".

If you want to do more complex stuff, you might want to create a function. The equivalent function "function.test.php" would look like this (the file would need to live in CMS_ROOT/plugins):

Code: Select all

<?php

function smarty_cms_function_test($params, &$smarty)
{
        global $gCms;
        $foo = &$gCms->variables;
        $foo['number'] = 4;

}
function smarty_cms_help_function_test() {
        echo 'whatever Help info';      
}

function smarty_cms_about_function_test() {
        echo 'whatever author info';
}

?>

Hope that helps!

___Samuel___