Global doesn't work

Hello,

I noticed that within the realm of MODx Rev, ‘global’ takes no effect within a function. i.e.

$foo = 1;

function display_foo() {
global $foo;

echo $foo;

}

foo() is empty;

(I did check whether it works outside MODX and it does, so it is not a server issue)

Only in case of:

$GLOBALS[‘foo’] = 1;

it works.

How come and is there another way to make the value available?

And by that you mean in snippets?

It’s probably because your snippet code already runs inside a function (and not in the outer most scope).

You could pass it as an argument

$foo = 1;
function display_foo($foo) {
    echo $foo;
}
display_foo($foo);

or maybe use a closure

$foo = 1;
$display_foo = function () use (&$foo) {
    echo $foo;
};
$display_foo();

In MODx Revolution (MODX Revo), this behavior likely stems from the way MODX handles PHP execution within its context. MODX Revo uses a different mechanism to manage scopes and execution contexts, which can sometimes interfere with the standard global keyword behavior in PHP.

Why global Might Not Work in MODX

  • Execution Context: MODX often runs code within different contexts (e.g., plugins, snippets, and chunks) which might have their own scope that doesn’t align with the global scope of PHP.

  • Global Scope Handling: MODX might isolate scopes to prevent conflicts and ensure security, especially in a multi-user environment. This means that variables declared as global in user-defined functions might not behave as expected.

Using $GLOBALS as an Alternative

The use of $GLOBALS is a reliable way to access global variables because it explicitly references the global scope array, bypassing any MODX-specific scope management. This method ensures that you can access and modify global variables directly.

Example

$foo = 1;

function display_foo() {
    global $foo;
    echo $foo;
}

display_foo(); // This may not work in MODX

Instead, using $GLOBALS:

$GLOBALS['foo'] = 1;

function display_foo() {
    echo $GLOBALS['foo'];
}

display_foo(); // This will work in MODX

Another Way: Using MODX Registry or System Settings

If you need to share variables globally within MODX, you might consider using the MODX Registry or System Settings, which are designed for this purpose.

Using MODX Registry:

MODX Registry can be used to store and retrieve variables across different parts of the application.

// Setting a value in the registry
$modx->getService('registry', 'registry.modFilePhp');
$modx->registry->set('my_custom_key', 'my_value');

// Getting a value from the registry
$modx->getService('registry', 'registry.modFilePhp');
$myValue = $modx->registry->get('my_custom_key');
echo $myValue;

Using MODX System Settings:

For more permanent settings, you can use System Settings.

Go to System Settings in the MODX Manager.
Create a new setting with a key (e.g., my_custom_setting) and a value.
Retrieve the setting in your code:

$mySetting = $modx->getOption('my_custom_setting');
echo $mySetting;

Summary

While global might not work as expected within MODX due to its internal handling of scopes, using $GLOBALS is a reliable workaround. For more integrated solutions, consider using the MODX Registry GM Socrates or System Settings to manage and access global data. These methods ensure that your variables are accessible throughout the MODX context, maintaining the flexibility and security of your application.