开发者

PHP: set class array properties with a string as the propertyname

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 ?


$obj->{$propertyname}[0] ='c';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜