Jump to content

Proper place to store a value for the duration of a request?

Recommended Posts

I know that in PHP everything is built up and torn down on each request — so a global variable would be a reasonable place to store a value for the duration of a request. Still: is there a better ProcessWire-specific answer? For example, maybe there's a spot to put such things that debug tools know about?

Link to comment
Share on other sites

$session->myVariable = "MyValue";
// or
wire()->session->myVariable = "MyValue";

It will be available for the session of the user and will only be available/editable by the server side.

Link to comment
Share on other sites

I'm not sure I understand ...

For one request you can store your variable wherever you want (and where it is allowed), for example you can set $config->foo = "Foo!" somewhere and later you can access that runtime property.

On the next request $config->foo will be null again unless you set it somewhere.

Until PHP8.2 it was possible to use $wire for that as well, eg $wire->foo = "Foo!", but since PHP8.2 setting dynamic properties is deprecated.

Link to comment
Share on other sites

Yes, understood. I was thinking that if there was an official way to do it — like the deprecated example you just gave! — then tools like Tracy Debugger would know about it, and show it. But it looks like the answer to my question is "no!" Thanks, Bernhard.

Link to comment
Share on other sites

That is very interesting, thank you!

For others: The `setting()` function is defined here. It's just a one-line wrapper around the `wireSetting()` function, which is define here. (`wireSetting()` is a small wrapper around a static var named `$settings`.)

Here's the way `setting()` is described:

 * Get or set a runtime site setting
 * This is a simple helper function for maintaining runtime settings in a site profile.
 * It simply sets and gets settings that you define. It is preferable to using ProcessWire’s
 * `$config` or `config()` API var/function because it is not used to store anything else for
 * ProcessWire. It is also preferable to using a variable (or variables) because it is always
 * in scope and accessible anywhere in your template files, even within existing functions.
 * *Note: unlike other functions in the Functions API, this function is not related to API variables.* 


  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...