PHP中的$ this-> a和$ this - > $ b之间的区别

Joh*_*nny 4 php oop

有什么区别:

$this->$a$this->b

在我的班上,我有这个:

class someClass{
    public $a;
    function aFunction(){
      $this->a = 5;
      $this->$b = 7;
    }
 }
Run Code Online (Sandbox Code Playgroud)

什么让额外的'$'做了 $this->$b

aor*_*sik 11

有很多不同之处:

$this->a$a你班级的财产

$this->$b另一方面通过名称引用属性,该属性$b作为字符串存储在变量中:

$b = "a";
$this->$b equals $this->a

$b = "hello"
$this->$b equals $this->hello
Run Code Online (Sandbox Code Playgroud)