We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45118
    • 123 Posts
    What is the best way?
    $prop = $scriptProperties['propertyName'];
    or
    $prop = $modx->getOption('propertyName', $scriptProperties);

    And why?

    Thanks in advance for your input!
      • 3749
      • 24,544 Posts
      The second, because the first will throw a PHP E_NOTICE error if that property is missing.

      You could also do this, but getOption() is a lot cleaner and clearer:

      if (isset($scriptProperties['propertyName'])) {
          $prop = $scriptProperties['propertyName'];
      } else {
          $prop = 'some default value';
      }


      getOption() also gives you more flexibility with its last two arguments:

      $prop = $modx->getOption('propertyName', $scriptProperties, 'default value', true);


      The default value will be used if the property is not set anywhere (tag, default properties, property set). This bit me hard long ago because if the property exists in the default property set, isset() will return true, even if the property is empty and has never been given a value.

      The fourth argument, if true, tells getOption() to use the default value if the property is set but empty. That's handy if the property exists in the snippet's default properties (or anywhere else) but is empty. I use it often, unless 0 or an empty string is a possible valid value.

      The third argument is null if you omit it. The fourth argument is false if you omit it.

      FYI, it's not likely, but if the property is not set, getOption() will check SystemSettings, User Settings, Context Settings, and User Group Settings for the key and return the value if it finds a match. Moral: make sure your property names are not the same as any Setting names (e.g., emailSubject). Using a prefix on your properties will make sure that doesn't happen.




        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