1

I have a class with a property that is an array:

  class NewObject {
    public $Props = array();
  }

  $obj = new NewObject();

  $obj->Props[0] = 'a';
  $obj->Props[1] = 'b';

Now I want to change the values of Props, not directly, but with a variable 'propertyname': This DOES work for single string properties but not for arrays, because the key N is interpreted as the Nth letter of the STRING 'Props' instead of the Nth value in the array!

  $propertyname = 'Props';

  $obj->$propertyname[0] ='c';   //doesnt work as expected, it tries to set $obj->P now, it seems
  $obj->$propertyname[1] ='d';

Any way to solve this ?

1 Answer 1

5
$obj->{$propertyname}[0] ='c';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx...I knew about the accolade thing but only in double-quoted strings in which you want to use a variable... never seen it like this before...

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.