We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37054
    • 93 Posts
    Pretty sure this is an issue of scope. I have a snippet with various functions. By both using the global keyword or by passing in the $modx object as a parameter to my function I can access all its methods with no issue.

    Where I'm having a problem is accessing static constants such as modX::LOG_LEVEL_ERROR so I can write to the error log in the course of executing a function. If I call $modx->log(modX::LOG_LEVE_ERROR,'test') inside a function I get no log entry. However, if I call $modx->log('1,'test'); I get my expected output in the log so it does seem to me that my function can't access the constant - how to get around this is the question. Ideas? [ed. note: freelancewebdev last edited this post 14 years, 5 months ago.]
      • 8386 ☆ A M B ☆
      • 160 Posts
      Hi FreelanceWebDev, I hope this is just a typo in your post and not the same in your code, else this could be the issue.
      $modx->log(modX::LOG_LEVE_ERROR,'test')

      Notice the missing L? otherwise try
      $modx->log(xPDO::LOG_LEVEL_ERROR,'test')
        • 37054
        • 93 Posts
        Thanks but no, was just a typo there. The problem is that when you refer to modX::LOG_LEVEL_X you are calling a static constant member of the modX class - the modX class isn't initialised - due to scope, PHP can't find the class.

        Fortunately, the solution is simple, you can also call static constants on a instantiated class so in this case you just use $modx->LOG_LEVEL_X rather than the static equivalent.
          • 22303 MODX Staff
          • 10,725 Posts
          Once the modX class is included, all the constants are available to you. This is not a scope issue unless you have not included the modX class yet.
            • 3749
            • 24,544 Posts
            Your tweet said is was in a snippet. If that's the case $modx and its contants are always available in one of these ways:

            myFunction() {
            
            global $modx;
               $modx->whatever();
            }




            
            myFunction($modx);
            
            myFunction( &$modx) {
               $modx->whatever();
            } 



            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 5 months ago.]
              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
              • 37054
              • 93 Posts
              Right, thought I was making progress but it seems not. So let me go through this again and maybe someone can explain - this is more of a PHP issue than MODX but in any case I'm sure it will be helpful to others.

              I have a simple snippet. In my snippet I have a function to which I make the $modx class instance available - like this

              function doStuff(){
              global $modx; # I make the modx class available to the internal scope of the function
              # if I then try to call
              $modx->log(modX::LOG_LEVEL_ERROR,'some string');
              # the call fails silently
              # if I call 
              $modx->log(1,'some string');
              #I get the expected output in the log - i.e. 'some string'
              #so I assume that my function isn't able to access the static modX call with the scope resolution
              # operator
              # From reading this (http://www.sunilb.com/php/php-5-tutorials-static-data-members-and-methods)
              # it seems that I should be able to call 
              $modx->log($modx->LOG_LEVEL_ERROR,'some string');
              # and get the desired result but instead I get an error output as if I had set LOG_LEVEL_FATAL
              }
              


              So essentially I guess what I am asking here is how can I access those static constants in the function scope. I can stick them in an array outside the function and pass them in via the global keyword or function params but I'm wondering if there is another way.
                • 3749
                • 24,544 Posts
                OK, you apparently have the $modx object, but not the constants (which seems impossible since they are part of the xPDO class and the modX class extends it).


                It shouldn't make any difference, but try this:

                $modx->log(xPDO::LOG_LEVEL_ERROR,'some string');
                  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
                  • 37054
                  • 93 Posts
                  Inside the function xPDO::LOG_LEVEL_INFO evaluates to 3 so some progress but still no output in log file when I pass xPDO::LOG_LEVEL_INFO to the $modx->log() function - hmmmmmmmz
                    • 37054
                    • 93 Posts
                    Right, both xPDO::LOG_LEVEL_ERROR and modX::LOG_LEVEL_ERROR are both available in my function, but when I pass either to the $modx->log() function I get nothing in the error log...
                      • 37054
                      • 93 Posts
                      Ahhhh, OK, I see what you were getting at now Bob on Twitter - so if I understand you right, I can only put entries of modX::LOG_LEVEL_ERROR in the error log, not modX::LOG_LEVEL_INFO.