We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48243
    • 12 Posts
    I am trying to create a general global object variable like '$modx' so I don't have to create new instances in snippets. I am having trouble finding where to place it. I tried making a plugin that fires certain events like onhandlerequest and onpageinit but it still cannot be accessed. I tried creating an instance of the object in the index.php file as I see that is where $modx instance is created, but same result and I cannot access this object.

    Any suggestions on how to create global variables/ object variables that can be accessed throughout the application/site without the constant instantiation?
      • 3749
      • 24,544 Posts
      The usual method is to create a new System Setting and use one of these methods to get it:

      in PHP:

      $value = $modx->getOption('settingName', null);


      Or in HTML:
      [[++settingName]]


      You could also just use a file and either 'include' it or read it.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 48243
        • 12 Posts
        Thanks for the response BobRay,

        I do agree with those options. But, I am looking to access the object without using modx->getOption() or modx tags.

        I mainly will be using the object in snippets.

        So for example we can access $modx throughout all pages without the necessity of putting

        $modx = new modX();
        


        I would like that same convenience for this variable like so:

        <?php 
        //example
        
        //modx object
        $modx;
        
        //my object
        $app;
        
          • 3749
          • 24,544 Posts
          The problem is that you have to instantiate your object on every page load.

          You can add your object to $modx:

          $modx->app = $app;


          Then refer to it anywhere:

          $modx->app;

          The nice thing about System Settings is that they're in the DB, so $modx->getOption() can get them anywhere $modx exists with no extra work.

          If the objects can be persistent classes, you can create them as packages and use (IIRC) $modx->addExtensionPackage() just once to register them. After that, they'll be stored and the package will be loaded automatically on every page load and you can use something like:

          $app = new 'App';


          I believe Articles does this. I know ClassExtender does.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 18373 ☆ A M B ☆
            • 3,141 Posts
            If you use $modx->getService, your object is stored in $modx->servicename so it's available where-ever MODX is. You could call getService in a plugin OnHandleRequest and it should be available anywhere after that.

            $modx->addExtensionPackages like Bob said is also useful, that could replace the call to getService in a plugin as the extension package is automatically loaded when MODX is instantiated. From memory I'm not sure if that also adds it as $modx->servicename though..

            As for $modx being a "global" variable.. it isn't actually a global in the php-global-way most of the time, it just gets passed around in elements and events.
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.