Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $this->a and $this->$b in PHP

Tags:

oop

php

What is 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;
    }
 }

what does having the extra '$' do in the $this->$b

like image 832
Johnny Avatar asked Dec 21 '22 18:12

Johnny


1 Answers

There is a lot of difference:

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

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

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

$b = "hello"
$this->$b equals $this->hello
like image 76
aorcsik Avatar answered Jan 02 '23 05:01

aorcsik