Jump to content

[Probably daft] performance question: _init.php Vs. config.php for vars


alan
 Share

Recommended Posts

Hello,

I am loving using _init.php to set stuff up and keep some constant info DRY.

However I think I ran into a situation while I was working with Form Builder where I was (legally/sensibly) editing a file the Module uses to send emails and cheerfully went about referring to a var that I knew I had established in _init.php only to find it was not being found.

I quickly realized this was because the file in question was not a regular PW Template file (but a Form Builder support file).

But it left me wondering which is the best approach for globally defining vars:

  1. set them all in config.php so I am guaranteed that from *any* file I can access them by `wire("config")->myVar`
  2. set them all in _init.php and for those I can't access in rare cases like Form Builder files do something special like for those vars define them in config.php and have _init.php set a var so all normal Templates can access the same way
  3. set them all in config.php then in _init.php read them all from config.php and re-declare them as named vars (i.e. same as 2. but for all vars not just those that are needed by non-Template files)?

I'm leaning toward 3. but any thoughts (particularly to do with not wanting to inadvertently bog down PW by doing something silly) would be most appreciated.

Link to comment
Share on other sites

set them all in config.php so I am guaranteed that from *any* file I can access them by `wire("config")->myVar`

This is a good way to go, and exactly what I do for predefined settings like required image dimensions and such. 

set them all in _init.php and for those I can't access in rare cases like Form Builder files do something special like for those vars define them in config.php and have _init.php set a var so all normal Templates can access the same way

What you set in _init.php is for runtime use of your site's template files, and probably nothing beyond that. These variables are good for initializing placeholders that you'll be outputting in your _main.php (or whatever your appendTemplateFIle is called). 

set them all in config.php then in _init.php read them all from config.php and re-declare them as named vars (i.e. same as 2. but for all vars not just those that are needed by non-Template files)?

This sounds like overkill to me, though of course use what works best for you. But you have a couple more options to consider:

Use session variables, especially when you want the value to still be present and available on not just the current request, but subsequent ones (from the same user) too: 

// do this 
$session->set('my_variable', 'my_value'); 
// then retrieve it from anywhere (and at any later request) like this: 
$my_value = $session->get('my_variable');  

Set an API variable:

// do this
wire('my_variable', 'my_value'); 
// then retrieve it from anywhere like this: 
$my_value = wire('my_variable'); 

Whichever route you take there, if you've got a lot of related values to set you might want to bundle them all into an array, so that you only need to set 1 session, API or config variable. 

  • Like 4
Link to comment
Share on other sites

This

Set an API variable...

and this

Use session variables, especially when you want the value to still be present and available on not just the current request, but subsequent ones (from the same user) too...

are two things I have missed in the past. This is one of the best things about PW, that new knowledge like this is so applicable and powerful because while some of it directly solves a question also so often I am able to take away general learning and do more with it than the sum of the parts. So much better than just learning what a new proprietary tag in some other CMS does.

Thank you very much Ryan.

  • Like 1
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
 Share

×
×
  • Create New...