We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3287
    • 12 Posts
    Hi,

    I’m working on creating some dynamically created CSS, and I’ve run into an issue.

    Here is my setup:
    - my template includes a ccs page -- a text only page from the modx manager
    - the css page calls the [[cssWrite]] snippet
    - the [[cssWrite]] snippet contains the following code:

    function tester(){
      $testout = $modx->runSnippet('testsnip',array());
      // testsnip code is simply: 
      // echo "Hello!  The call to the testsnip snippet worked!";  
      echo $testout;
    }
     tester();
    


    I get the error:
    [tt]
    Fatal error: Call to a member function runSnippet() on a non-object in /public_html/modx/assets/snippets/cssWrite/cssWrite.inc.php on line 75
    [/tt]

    if I run the contents of the tester function above (without it being part of a function), it works fine.
      $testout = $modx->runSnippet('testsnip',array());
      echo $testout;
    


    It works, and I get:
    [tt]
    Hello! The call to the testsnip snippet worked!
    [/tt]

    I’m relatively new to PHP and MODx. Although I’ve found other people with instances of this problem, I haven’t really been able to get this working. An help would be much appreciated!
      • 22303 MODX Staff
      • 10,725 Posts
      Either pass $modx to your function (by reference):
      <?php
      function tester(& $modx){
        $testout = $modx->runSnippet('testsnip',array());
        // testsnip code is simply: 
        // echo "Hello!  The call to the testsnip snippet worked!";  
        echo $testout;
      }
       tester($modx);
      ?>

      or declare $modx to be global in the scope of your function:
      <?php
      function tester(){
        global $modx;
        $testout = $modx->runSnippet('testsnip',array());
        // testsnip code is simply: 
        // echo "Hello!  The call to the testsnip snippet worked!";  
        echo $testout;
      }
       tester();
      ?>

      I encourage you to learn more about the scope of PHP variables.

      Cheers
        • 3287
        • 12 Posts
        Thank you for the solutions and the link!

        Perfect.