We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10323
    • 35 Posts
    I’m having trouble getting the module configuration parameters to work: They are not available inside functions in my module, even though I declared them global within those functions. One of the parameters looks like this:
    &mod_db=Database to use;string;my_db_name

    Outside functions $mod_db correctly echoes as ’my_db_name’.

    What’s wrong? Is this a bug? MODx 0.9.6 rev 2767.
      • 22303 MODX Staff
      • 10,725 Posts
      You’re definitely going to need to provide some code for context or explain more what you are trying to accomplish. MODx does not change PHP, so variable scope behaves normally, though you must remember that modules, snippets, and plugins are executed as eval()’s.
        • 10323
        • 35 Posts
        Ok, here is an example module code (with the module’s configuration parameter mod_db set to ’my_db_name’):

        $html = 'value of $mod_db from module: '. $mod_db .'<br />';
        $html .= get_mod_parameter();
        echo $html;
        
        function get_mod_parameter() {
          global $mod_db;
          $output = 'value of $mod_db from within a module function: '. $mod_db;
          return $output;
        }
        

        This returns:

        value of $mod_db from module: my_db_name
        value of $mod_db from within a module function:

        $mod_db is empty inside the function. I don’t see why.
          • 22303 MODX Staff
          • 10,725 Posts
          Again, without context of where this is being run or how, I have no idea where you are expecting this parameter to be available. In snippets, if passed as parameter, or inherited from a module, that should work. But I suspect you are expecting that parameter to be a global variable, and it’s not. It’s local to the scope of the module, and by referencing it using global, you are looking for something outside the current scopes of the function and module itself. I suggest passing the variable to the function. Globals are a bad practice IMHO, and you should avoid them like the plague.
            • 10323
            • 35 Posts
            This module is run inside the manager only. With it, one can manage another database, along the lines of http://www.sottwell.com/database-module.html"" target="_blank" rel="nofollow">this tutorial. The parameter I’m talking about is set in the module’s configuration tab.
            I suggest passing the variable to the function.
            There are several functions in the module that need the db name for building queries. I can pass this variable, ok, I just thought there was an "easier" way for module functions to access these configuration parameters. Like Susan stated:
            Now you will be able to use that as a variable anywhere in the module you need it.
            Thanks for your patience wink
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              But don’t forget that if your module uses a function, you have to either pass the variable in the function call, or have a line in the function declaring the variable to be global (global $var).
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 10323
                • 35 Posts
                have a line in the function declaring the variable to be global
                That’s what I did and what didn’t work (see my code example). I now pass the variable to the function.