I’ve researched that using global is considered bad practice as it allows the function to execute without the variable actually being set and CONSTANT‘s shouldn’t be used for anything that is very, very static such as the number of days in a week.
The problem I’m really frustrated with is using variables in functions and practicality.
For instance, let’s say I get the user’s IP with $_SERVER["REMOTE_ADDR"] and it’s needed in multiple functions. When I want to use one of those functions, I have to look back at the function and remind myself what the order was for the parameters, even though I know it is needed for the function.
Another instance is user information that isn’t going to be changing, like their username or their age. I need to use it in multiple functions, but again I have to figure out the order of the parameters and include about 5 extra parameters for all the information, it honestly seems counter-productive to follow good practices.
Am I getting this right? Do you guys usually just include all the parameters?
You might find it helpful to look into the Registry pattern.
Personally, if a method needs access to an authenticated user’s details my code tends to look something like: