1

I have got a php class named request. Is it possible to access one of it's properties with a variable in the name of a property?

Something like this:

$i=4;
$request->option_$i = 'something';

Thank you

3
  • Demo Commented Sep 7, 2015 at 15:26
  • 3
    Why why why? This kind of thing should be in an array, not a dynamic variable name. Yes you can do it, but it's really bad coding practice. Just turn it into an array and reference it as $request->option[$i]. Commented Sep 7, 2015 at 16:22
  • Thank you, for your answer! Commented Sep 7, 2015 at 16:40

1 Answer 1

3

You can do it like this:

$request->{'option_'.$i} = 'something';
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, yeah, but your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should. - Dr. Ian Malcolm

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.