E
E
eugene1592019-11-28 23:14:17
MODX
eugene159, 2019-11-28 23:14:17

Why use getOption in modx if the variable is already available?

There are articles on the Internet where the code of some snippets is given. And in the code, the snippet parameters are obtained like this:

$word = $modx->getOption('word', $scriptProperties, '');

My question is: why use it if $word is already available?
I wrote this code on the main

page: And here is the code for my_snippet snippet:
<?php
echo $word;

And everything works great. Why then write constructions like this?
$word = $modx->getOption('word', $scriptProperties, '');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-11-29
@i__dmitry

In most cases, there is no difference.

$word; // Hello
$scriptProperties['word']; // Hello
$modx->getOption('word', $scriptProperties, ''); // Hello

But, if you read the documentation on the getOption function , you will notice that this function first checks for the presence of the key from the first parameter in the array specified by the second parameter, if the key is missing, it looks for the presence of this key in the MODX system settings, and only then, if it is missing and setting, returns the value from the third parameter. This can be useful if your package has default settings stored in the MODX system settings that can be overridden via a snippet call.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question