0

I have a little issue:

<?=$this->bs_forms->text_input('last_name','Name', $user->last_name);?>

If the last variable $user, does not exist I currently get some PHP errors. My solution was this:

<?=$this->bs_forms->text_input('last_name','Last Name', (isset($user))? $user->last_name : NULL);?>

But that's seems a bit hacky. Is there a better way to do this?

2
  • why necessary in function call ? Commented Sep 14, 2013 at 21:05
  • This question would be improved by adding the exact error message which you receive. This would allow the possibility of people with similar issues to find your question (and the associated answers) in the future. Commented Sep 14, 2013 at 21:43

4 Answers 4

1
//Ultimate    
         (isset($user))? $user->last_name : NULL)
//Best
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think this has anything to do with optional arguments. The question is about dealing with a variable in the caller that might not be set.
Yes it does. It shows code that calls the function text_input(). The issue is that $user might not be set when making that call, which causes an error when he tries to pass $user->last_name as an argument.
I am calling text_input(). If $user->last_name is set it will use this to populate a form field.
So the solution I am using is the best ?
0

You can use PHP's error-suppression modifier:

<?=$this->bs_forms->text_input('last_name','Name', @$user->last_name);?>

Comments

0

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.

3 Comments

I think you're making this seem worse than it is. It's not uncommon to leave a variable like this unset, and to allow the code to default reasonably. The text_input() function probably uses that argument as a default value in an input field; if it's not set, there's just no default.
The reason it may be empty is that I am usign the same form twice. 1. for user registration, in which case there will be no values for the user, as the user will not have an entry in the Db yet. 2. if a registered user wants to change his accounts details I load the form and populate it with the values stored in the DB, the user details are stored in the $user object.
It is generally considered bad practice to reference a variable which "may or may not" be defined (this is why PHP gives an error, rather than ignoring it). Explicitly specifying a behaviour in situations where the value has not been set, rather than relying on the internals of both the programming language and the function which is being called, makes code more readable. Using good practice, rather than rote boilerplate to discard errors, also prevents actual errors (most commonly in this case: typos) from slipping through.
0

(isset($user))? $user->last_name : NULL)

Here the error is possible - "Trying to get property of non-object"

Best way:

(isset($user->last_name))? $user->last_name : NULL)

PS. Use @ - bad practice

1 Comment

If your code has the invariant $user is either undefined or it's set to a User object, the two forms are essentially equivalent. You probably want an error to be signalled if $user is set to something other than a User.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.