We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26903
    • 1,336 Posts
    undecided
    Hello,

    I’m converting a handcrafted html/php site into a Modx hosted one, so far all is well, I’m fairly pleased with Modx, however I have
    come across this problem.

    I am including an external php file from a snippet, in this file I have the following construct(abbreviated for clarity):-

    function Generate()
    {
    Line 1
    Line 2
    return $output
    }

    //Main entry point
    if ( isset(somevariable))
    {
    $chunkoutput="Default"
    $chunkoutput = Generate();
    $modx->setPlaceHolder(’mygeneration’,$chunkoutput)
    }

    Ok, this always returns null for $chunkoutput, if I comment out the call to Generate() it correctly returns the string "Default" as I’ve set.
    If I comment out the call to Generate() and include the function body inline ie place Line 1, Line 2 above the placeholder call it also produces the correct output.

    Why is the output variable set to null if I use a function call here?

    No errors in the error-log of any kind, am I missing something obvious here? Can I turn on some debug inside modx to show me the actual code its generating?

    Any pointers please. This is a bit of a show stopper at the moment.

      Use MODx, or the cat gets it!
      • 2734
      • 165 Posts
      Hm.

      Could it be that the inclusion doesn’t work? Did you try "require" instead of "include", that way you should get an error if the file wasn’t found.
      Or you could check if that function really exist by writing something like that:
      if(function_exists('Generate')){
          echo 'Function found';
      } else {
          echo 'Function not found. Inclusion error?';
      }
      
        • 26903
        • 1,336 Posts
        Thanks for the reply.

        yes, I might try the wrapping test to see if it sheds more light on this, but as can be seen from the code above the function itself is not separately included it is declared only a few lines above its invocation in the same file, I’m sure its being called as if i comment it out the value of $output is "Default", if I comment it in its null, so it is being changed by something.

        I’ll do some further investigation
          Use MODx, or the cat gets it!
          • 26903
          • 1,336 Posts
          Ok sussed it.

          The function was using a global variable that for some reason was null inside the function itself(dont know why, existing code uses ’global’ decs correctly etc. no errors reported by modx or apache)
          If I pass the global as a parameter at the point that it is valid in the main program ie like Generate($word) and use this inside the function it all works. This is better than the original code as it tightens up on the scoping here.

          The rest of the conversion and import shoulde be fine now, looking forward to doing this now, fairly impressed with Modx so far.
          smiley
            Use MODx, or the cat gets it!
            • 3749
            • 24,544 Posts
            I’ve had the same problem a few times where a global variable wasn’t accessible inside a function even with the global declaration. I wish I knew what caused it. Included PHP files are supposed to have access to all the variables in the including file but it doesn’t always work for me.


            Bob
              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
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Don’t forget that the snippet itself is being run through the PHP eval() function. I rather imagine that this can confuse the issue. 0.9.7 eliminates the use of eval(), so it will make a big difference.
                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
                • 3749
                • 24,544 Posts
                Quote from: sottwell at Feb 08, 2008, 01:39 AM

                Don’t forget that the snippet itself is being run through the PHP eval() function. I rather imagine that this can confuse the issue. 0.9.7 eliminates the use of eval(), so it will make a big difference.

                Aha. You’d think I’d know that by now. embarrassed

                Yet another reason I can’t wait for the official 0.9.7 release. smiley

                Bob
                  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
                  • 26903
                  • 1,336 Posts
                  Yes, thanks for the info its just one of those little ’gotchas’ that you either know or don’t, especially in that I am importing existing working code so you expect it to work. In this case its actually improved the code as it forces you to tighten up on these things, however for people that cant work round it it a bit of a shocker!
                    Use MODx, or the cat gets it!