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.
------------------------------------------------------------------------------------------
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.]