我设置了一些变量:
$field = "XYZ";
$block_hi = $field."_hi";
$block_lo = $field."_lo";
然后我有一个对象,其属性具有上述变量的名称:
$obj->XYZ_hi['val'] = "value1";
$obj->XYZ_lo['val'] = "value2";
我以为我可以使用PHP的变量来引用属性:
print( $obj->${$block_hi}['val'] );
print( $obj->${$block_lo}['val'] );
我希望得到:
value1
value2
但是这些行会在apache的error_log中抛出错误:
PHP Fatal error: Cannot access empty property in script.php
答案 0 :(得分:2)
这会有效,你有双$$,在这个例子中不需要。
$field = "XYZ";
$block_hi = $field."_hi";
$block_lo = $field."_lo";
print($node->{$block_hi}['val']);
print($node->{$block_lo}['val']);