We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53742
    • 24 Posts
    Simple Snippet for 'global' Directive Test:
    <?php
    $z = 'z';
    function ff()
    {	
    	global $z;
    	var_dump($z);
    	$z = 'not z';
    	var_dump($z);
    }
    var_dump($z);
    ff();
    var_dump($z);
    ?>


    if we name it as 'test' and include into something as [[!test]] it will output following Text:
    .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:10:string 'z' (length=1)

    .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:6:null

    .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:8:string 'not z' (length=5)

    .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:12:string 'z' (length=1)

    This means that 'global' Directive is NOT working!
    How is it possible and how to enable 'global' Directive?

    Thanks.

    P. S.

    PHP Version 7.0.22-0ubuntu0.17.04.1
    MODX Revolution 2.5.8-pl

    This question has been answered by tester3. See the first response.

    • discuss.answer
      • 53742
      • 24 Posts
      Seems like if i add 'global' Directive in outer space, it works.

      In other words,
      this Code
      <?php
      global $z;  //  <- Added an outer 'global'
      $z = 'z';
      function ff()
      {	
      	global $z;
      	var_dump($z);
      	$z = 'not z';
      	var_dump($z);
      }
      var_dump($z);
      ff();
      var_dump($z);
      ?>
      


      shows
      .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:11:string 'z' (length=1)

      .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:7:string 'z' (length=1)

      .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:9:string 'not z' (length=5)

      .../modx/core/cache/includes/elements/modsnippet/47.include.cache.php:13:string 'not z' (length=5)

      That is strange.
      Does that mean that MODX somehow pre-processes the Snippet's Code?
        • 3749
        • 24,544 Posts
        I don't know what causes it, but I've had a number of occasions where global inside a snippet didn't work. I always pass $modx as an argument, which also cuts down the chance of collisions.
          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
          • 53742
          • 24 Posts
          Quote from: BobRay at Oct 06, 2017, 12:58 PM
          I don't know what causes it, but I've had a number of occasions where global inside a snippet didn't work. I always pass $modx as an argument, which also cuts down the chance of collisions.
          What do you mean? How does the $modx argument help? Is there any way to use "local" "global" variables (excuse me for this name smiley ) from it? Something like $modx->getGloablVariable('xyz') ?
            • 3749
            • 24,544 Posts
            Sorry, I wasn't clear. I meant using $modx as an argument rather than global $modx in the functions.

            Other than $modx, I don't use global variables at all to avoid collisions and polluting the global PHP namespace.

            Most often, I create a class and make them class variables so they'll be available throughout the class and nowhere else.
              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