-
I've read through the issues I found regarding PHP 8.1 (#605, #617, probably a couple others) and I'm still not sure what exactly I should do about 8.0+ raising this particular notice to a warning. Say I have the following code:
It compiles to:
Which will obviously throw a warning if I never assigned $switch anywhere. Normally, if I were writing this in PHP I'd have an isset() check beforehand, or if I knew it was an array I'd have an array_key_exists(). Unfortunately, isset() doesn't work because it checks for that same array key:
Using the default modifier also doesn't stop the warning from happening:
Now, I understand that I can use $smarty->muteUndefinedOrNullWarnings() to shut up the log, but I'd really like to know the proper way to code this. Do I have to make absolutely sure that every single variable I use is pre-assigned with a value just in case nothing else sets it elsewhere (such as the above example which is exists/true or not-exists/false)? That's slightly inconvenient, because in the template it looks like a primitive but in reality it's a key in an array that has additional checking required (array_key_exists). Would it be simple enough to add an "exists" modifier that could wrap that properly? So something like:
Or can Smarty just handle that properly somehow so it does that automatically? It would seem that default behavior would be that if it doesn't exist (undefined), PHP considers it empty and null, which will loosely equate to false, 0, etc. I would expect it to compile to something like:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The way I see it, you are already doing it fine. It's probably good practice to make sure you always provide a value for each variable the template uses. If you forget, you'll get a warning. Not a fatal error. Mute the warnings if you don't want to see them in your production logs. |
Beta Was this translation helpful? Give feedback.
The way I see it, you are already doing it fine. It's probably good practice to make sure you always provide a value for each variable the template uses. If you forget, you'll get a warning. Not a fatal error.
Mute the warnings if you don't want to see them in your production logs.
It can be useful to see the warnings during development, though.