且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

访问 PHP 对象属性的语法是什么?

更新时间:2023-11-20 19:45:10

  1. $property1//具体变量
  2. $this->property1//具体属性
  1. $property1 // specific variable
  2. $this->property1 // specific attribute

类的一般用途是不使用 "$" 否则你正在调用一个名为 $property1 的变量,它可以接受任何值.

The general use on classes is without "$" otherwise you are calling a variable called $property1 that could take any value.

示例:

class X {
  public $property1 = 'Value 1';
  public $property2 = 'Value 2';
}
$property1 = 'property2';  //Name of attribute 2
$x_object = new X();
echo $x_object->property1; //Return 'Value 1'
echo $x_object->$property1; //Return 'Value 2'