【发布时间】:2015-07-19 17:49:38
【问题描述】:
我在对象中有数组,在变量中有他的名字。当我请求 $object->$propertyWithArrayName 时,它的返回数组很好,但是当我想从该数组的索引处获取值时它不起作用。
代码:
class Foo {
public $bar;
public function __construct() {
$this->bar = array("A" => "a", "B" => "b");
}
}
$test = new Foo();
$propertyName = "bar";
var_dump($test->$propertyName); // ok
var_dump($test->$propertyName["A"]); // doesn't work
第二个 var_dump 引发 Warning: Illegal string offset 'A' 和 Notice: Undefined property: Foo::$b。
为什么它不起作用?
【问题讨论】:
标签: php arrays variables undefined