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';
精彩评论