On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi,
    my question seems too simple but i really don’t know when i must use GLOBAL $modx in my snippets.
    I thought that it need for calling MODx APIs but APIs works without it too.

    please guide me to solve my problem with codes.


    Thanks
    AHHP
      God loves me. 【ツ】


      MODX.ir (Persian Support)

      Boplo.ir/modx/ (Persian)
    • If you define a function in your snippet it needs to set the global because of variable scope. Variables defined outside of a function definition are not visible within that function, unless you declare the outside variable global.
      // snippet code
      ...
      function myFunction() {
          global $modx;
          
          $thisDocId = $modx->documentIdentifier;
          ...
          return $somevalue;
      }
      // more snippet code
      ...
      return $snippetvalue;
      


      If your snippet does not define a function, then it doesn’t need to have any global declaration for the $modx object, it’s available to the snippet code.

        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
      • oh you again! thanks a lot wink
        you have helped me very much in learning MODx smiley


        thanks again
        AHHP
          God loves me. 【ツ】


          MODX.ir (Persian Support)

          Boplo.ir/modx/ (Persian)
        • Or, put another way, without the "global $modx", PHP will will, by default, create a brand new variable called $modx that will be private to that function and the references to its member variables (e.g. $modx->documentIdentifier) will come back empty because it doesn’t have any.

          So the "global $modx" is just telling PHP that you mean the global variable $modx that was created outside the function.

          The short answer is that you need it whenever you use $modx-> inside a function.
            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
          • thank you BobRay too
            i understood well by your excellent answers laugh
              God loves me. 【ツ】


              MODX.ir (Persian Support)

              Boplo.ir/modx/ (Persian)
            • Hi guys!
              These advises didn't help me. Please let me know what I'm doing wrong. Here is the code:
              $resource = $modx->getObject('modResource', $id);
              function someFunc($tvname2decode) {
                  global $resource;
                  $object_from_tv = json_decode($resource->getTVvalue($tvname2decode)); // ← FAILS HERE
                  ...
              

              Tried also to pass $resource to function through function argument – failed. Tried to declare global $modx and then declare $resource within the function — failed... Seems like I'm missing some very simple but fundamental thing here...

              Please help!

              PS: I tried different combinations: problem is not in json_decode – simple $obj = $resource->getTVvalue fails too. And everything works fine when I process these TVs outside a function, but I need to repeat the process few times for different TVs, so using a function is a must.
              • Passing the $resource variable as an argument would be the better way.

                Make sure you actually have the resource, though:

                $resource = $modx->getObject('modResource', $id);
                if (! $resource) {
                   /* handle the error */
                }
                


                It may not be your problem, but I generally don't use $resource or $id in my code to avoid collisions:

                $doc = $modx->getObject('modResource', $docId);
                  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
                • Thank You Bob.

                  Unbeleivably fast response!!! smiley

                  Figured things out: typo in a function call, causing that $resource was not passed to function... Shame on me! And thanks a lot for the tip. Still wondering why global declaration within a function doesn't work. Though I passed the resource in through a function argument and now everything works. Thanks again!