在不使用中间变量的情况下访问PHP变量字段名称

时间:2012-12-11 15:40:21

标签: php

  

可能重复:
  Get PHP class property by string

有更清洁的方法吗?它没有编译

$object1->name = 'my_name';
$object1->address = 'address';
$object2->somefield->($object1->name) = $object1;

仅当我分配给另一个变量$ object1-> name

时,它才有效
$object1->name = 'my_name';
$object1->address = 'address';
$temp = $object1->name;
$object2->somefield->$temp = $object1;

1 个答案:

答案 0 :(得分:9)

试试这个:

$object2->somefield->{$object1->name} = $object1;