We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • When writing a snippet (or TV or anything else, for that matter) and you write a function, make sure to enclose the function definition in a conditional. For example, a function in a snippet named MySnippet could have:

    if(!function_exists(MSmyFunction)) {
      function MSmyFunction() {
     ...
      } // end MSmyFunction
    } // end function_exists
    


    If somebody wants to call your snippet more than once on the same page, or in a template, now they can. Otherwise, if a function is already defined by the first use of the snippet, PHP will throw an error when it’s defined again in the second use of the function.

    Also, it’s a good idea to somehow use your snippet name in all of your function names so there isn’t as much chance of somebody else’s snippet having a function of the same name that could cause a conflict.
      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
    • Very good point Susan... I did just that recently. I had to add a bit more as follows (the !function_exists(function-name) technique resulted in a function already declared error when used twice on one page):

      global $MemberCheck_defined;
      
      if (!isset ($MemberCheck_defined)) {
      
      	function MemeberCheck($groups,$chunk) {
      		global $modx;
      		
      		$check = $modx->isMemberOfWebGroup($groups);
      		... do more stuff here ...		
      		$o = ...
      		
      		return $o;
      	}
      	$MemberCheck_defined = true;
      }
      
      return MemeberCheck($groups,$chunk);


      This is also the way that DropMenu uses to be reused on the same page.

      Thanks for the tip!
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 50478
        • 1 Posts
        Amazing.. 9 years later, and this is still saving lives smiley Thank you Susan!
        • One other major function-in-snippets tip is that you need to explicitly declare $modx as global, otherwise your function won't be able to access any of the API functions or MODX properties.
            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
            • 37031
            • 93 Posts
            If I read this earlier it would've saved me 30 minutes of "WTF?!"

            Thanks for this Susan!
              • 3749
              • 24,544 Posts
              Still an excellent tip, but the function name should be in quotes:

              if(!function_exists('MSmyFunction')) {



              Otherwise, PHP will throw this warning:

              PHP notice: Use of undefined constant MSmyFunction

                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