PHP语法$ var1是什么?

以下语法的解释是什么?

$var1->$var2 // Note the second $


$var1是一个对象。

$var2 (可能)是$var1的一个变量的名称。

如果$var2="test"; 这被评估为:

$var1->test;

你可以用各种各样的东西做到这一点:

$test = array();
$name="test";
print_r($$name); // Prints array();

$test = new stdClass;
$test->hello = "hi";
$name2="hello";
echo $test->$name2; // Echos hi

你甚至可以变得很花哨:

echo $$name->$name2; // Echos hi

它意味着动态查询对象中的属性。

class A {
  public $a;
}

// static property access
$ob = new A;
$ob->a = 123;
print_r($ob);

// dynamic property access
$prop = 'a';
$ob->$prop = 345; // effectively $ob->a = 345;
print_r($ob);

所以$var1是某个对象的实例, ->意味着访问该对象的成员, $var2包含一个属性的名称。

链接地址: http://www.djcxy.com/p/1763.html

上一篇: What does the PHP syntax $var1

下一篇: Where do we use the object operator "