XF 2.2 \XF::options() as CONST?

Robert9

Well-known member
I guess that i need to param options->values to my templates, right?
And i need them in a lot of templates.
So it would be nice to have a const fot the class.

But it seems this is not possible?

class ...
public const XSADB = \XF::options()->xsa_db_key;
finctions ...

How can i solve this, please?
 
You can access options in templates via the global variables available to all templates: $xf.options.your_option_key. To see what variables are available in a template, you can include the following snippet: {{ dump(vars()) }}
 
Cool. Thank you.
Whats about const? It muss be a static value? Makes sense.
But how can i bring an options()->value to a whole class?
 
There's no downside to calling \XF::options() whenever you require it. You can't define dynamic runtime variables as constants though either way, or shouldn't, even if there's a loophole that makes it possible.
 
So ... you have classes with 20 functions; for all of them you need one CONST.
You have also 20 classes.

What you do now?
 
Load in every class the same option value. That what I would do.

Why do you need a const?

Otherwiese use
PHP:
define('MyConst', \XF::options()->yourOptionsKey);

If you don't want to write this 20 times, create a trait and add a use trait inside the class.
 
Ok, something new for me, a trait. Will look for that. Thank you.
...
ok, means to use a trait or class. Makes sense. ok.
 
Back
Top Bottom