We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I was going to suggest a change in getOption() that allowed a fourth argument to return the default if the value is empty or unset. Looking at the getOption() code reveals that it's already there. Testing revealed that it works perfectly. But I have two questions:

    1. Why are there two getOption() methods in xpdo.class.php with different signatures? This would be nice, but it's supposed to be impossible with PHP (and the second one is never called, regardless of the args).

    2. Why does getOption() execute 41 times (on a one-element array) before returning a value for the first call and only once for the rest with this code:

    (in the getOption() method)

     static $i = 1;
            echo "\n" . $i++;


    $props = array('my_key' => 'MyValue');
    
    $x = $modx->getOption('my_key', $props, 'Default');
    echo "\ngetOption1 - "   . $x;
    $x = $modx->getOption('my_key', $props, 'Default');
    echo "\ngetOption2 - " . $x;
    
    $props = array('my_key' => '');
    
    $x = $modx->getOption('my_key', $props, 'Default');
    echo "\ngetOption3 - " . $x;
    $x = $modx->getOption('my_key', $props, 'Default', true);
    echo "\ngetOption4 = " . $x;
    
    $x = $modx->getOption('xmy_key', $props, 'Default');
    echo "\ngetOption5 - " . $x;
    $x = $modx->getOption('xmy_key', $props, 'Default', true);
    echo "\ngetOption6 - " . $x;


    output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    getOption1 - MyValue
    42
    getOption2 - MyValue
    43
    getOption3 - 
    44
    getOption4 = Default
    45
    getOption5 - Default
    46
    getOption6 - Default



    As a side note, if people always use getOption() in their extras, MyComponent is going to do a much better job of finding the properties they use and checking them against their properties transport files. wink


    ------------------------------------------------------------------------------------------
    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, 1 month 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
      • 39932
      • 483 Posts
      Why does getOption() execute 41 times ... before returning a value?

      Wow! That's insane... An answer to this would explain quite a few things... I'm curious to know as well.

      ... but it's supposed to be impossible with PHP ...

      It can be done, but you have to be tricksy in a way that borders on sadistic. I've done it before and its not pretty. (It requires preg_replacing the code before it is interpreted by PHP and it brings up a nightmare of issues, performance just being one of them.)
        Website: Extended Dialog Development Blog: on Extended Dialog
        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
        • 34012
        • 88 Posts
        Hi Bob,

        I don't find two diff getOptions in the xPDO class. The second is in the same file but for xPDOConnection class.

        Also to test it bit more equally, for the output put also the key what the getOption is fetching, I would guess there wont be many duplicate keys fetched.

        Quite many of the xPDO methods use getOption to fetch stuff from xPDO/MODX::$config array. The method is just convenience method in general. You could do the same with $modx->config['MyValue'] which equals somewhat close to $modx->getOption('MyValue'); getOption just makes it easier to use the configs and with own array like with $scriptProperties with snippets. You don't need to create ternary operator / if else clause to check if key is null and do something if it is.



          Almost retired from web-development industry but still randomly writing at Lazylegs.info and on schedule hopefully in near future to finish Oracle and PostgreSQL ports of MODX
          • 3749
          • 24,544 Posts
          @lazylegs: OK, doh (sound of hand hitting forehead) on the first question. I should have looked more closely. I'm not used to seeing multiple classes in the same MODX file, but I can see why it's done here.

          On the second, though, I do provide the key for it to look for and it's looking at a one-element array, so it should find it immediately and have to look no further.

          On reflection, I can answer my own question. It's a new instance of MODX and it's initializing something (probably the System Settings values), or MODX calls getOption() 41 times when it's initialized. It happens when you instantiate MODX (which I'm doing above the code I posted). I'm too lazy to check, but I'm sure that it wouldn't happen in a snippet inside MODX. So double-doh. wink




          ------------------------------------------------------------------------------------------
          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, 1 month 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
            • 34012
            • 88 Posts
            Hehe,

            Obviously I had to test this myself, but I only use new instance of xPDO so got 2 + one of mine running through getOption()

            But gotta admit, had to read the source code if there is something funky going on. But no, it is solid from the xPDO side. Secondly I start to wonder why on earth I know about that behavior
              Almost retired from web-development industry but still randomly writing at Lazylegs.info and on schedule hopefully in near future to finish Oracle and PostgreSQL ports of MODX
              • 3749
              • 24,544 Posts
              Yes, I couldn't see any reason for it when looking at the code and was baffled until I realized that the echo I put in there would also be triggered by the initialization of xPDO and MODX.


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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