There are other ways of doing it, but they aren't any "better" than each-other. PHP is giving you an error message, and your proposed solution isn't actually fixing the problem, it's just adding a bit of code to avoid triggering it.
The actual error that PHP is trying to tell you about is that you have gotten yourself into a situation where you are trying to execute a line of code without knowing the state of those things you are passing into it. In short: PHP is complaining that $user is undefined, because you actually have not defined any value for $user.
There are several solutions here, of equal validity (and all essentially are different ways of doing the same basic thing):
- Ensure that this bit of code does not get called unless
$user is defined. I expect you actually have several similar lines, so you may want to divide all of these up into two larger "if $user is defined" and "if $user is not defined" sections.
- Load up an array of "default values" to pass in, and don't override those default values unless
$user is defined
- Ensure that
$user is always defined, but load it with default values if the real $user doesn't exist.