The difference between $ this-> a and $ this & # 8594; $ b in PHP

What's the difference between:

$this->$a and $this->b

In my class, I have this:

 class someClass{ public $a; function aFunction(){ $this->a = 5; $this->$b = 7; } } 

making the extra '$' in $this->$b

+4
source share
1 answer

There is a big difference:

$this->a refers to the $a property of your class

$this->$b on the other hand, refers to a property by name, which is stored in the variable $b as a string:

 $b = "a"; $this->$b equals $this->a $b = "hello" $this->$b equals $this->hello 
+11
source

All Articles