When to use getters?

Hi All,

Is it best to use,

$this->$myprivatevar
or
$this->get_myprivatevar()

when working with a private var within a class? What’s the best practise and why?

Thank you in advance…


class myclass{
 private $myprivatevar;

 function __constructor(){
     ...
     }

  public function set_myprivatevar($myprivatevar ){
     $this->myprivatevar = $myprivatevar ;
  }

  public function get_myprivatevar(){
     return $myprivatevar;
  }

  public function dosomething(){
  // is it better practise to use, $this->$myprivatevar
    return ($this->$myprivatevar != "") ? 'yes':'no';
  // or use a getter, get_myprivatevar()?
    return (get_myprivatevar() != "") ? 'yes':'no';
  }

     
  }

}

$myobj = new myclass();
$myobj->set_myprivatevar("...value...")
echo $myobj->dosomething();

I think, use $this->$myprivatevar is quite more clearly…

If you’re using the property internally, just use it directly I’d say.