0

In codeigniter, I retrieve database results and store it in a variable like the following:

$data['user'] = $this->some_model->get_by_id($id);

Say the above retrieves only the user_id and the user_name, so the variables will be

$data['user']->user_id;

$data['user']->user_name;

Now, say, I want to assign another value (eg. age), can I do the following?

$data['user']->age = '23';
5
  • 1
    Technically, yes, you can. But it's bad practice. Commented Jul 4, 2012 at 12:31
  • 1
    Yes, you can. Its only temporary. The actual database record does not change. Commented Jul 4, 2012 at 12:36
  • Thanks. Actually, the age will be calculated in PHP and used just to display it to the user by passing it to the view. I don't need the database record to change. @JohnConde Why is it bad practice? Does it have any adverse affect, besides just being mere bad practice? Commented Jul 4, 2012 at 12:44
  • Assigning values to undeclared object properties is "expensive" in PHP. Plus it is not always clearer to the reader where a value came from so this make code maintenance more difficult. Commented Jul 4, 2012 at 12:47
  • Making all of this an answer. Commented Jul 4, 2012 at 12:48

1 Answer 1

1

Technically, yes, you can. But it's bad practice. Assigning values to undeclared object properties is "expensive" in PHP. Plus it is not always clear to the reader where a value came from so this make code maintenance more difficult.

Sign up to request clarification or add additional context in comments.

Comments

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.