Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring php class member variables

Tags:

php

I was trying to write a class in such simple way

class piklu
{
   private $x=5;
   public function display()
   {
      echo $this->$x;
   }
}

but when after creating object of this class I'm calling the function display it is displaying an error unkown variable $x. Can any body suggest me what exactly I have to do to declare a private member variable in php.

like image 849
Piklu Guha Avatar asked Dec 08 '22 21:12

Piklu Guha


1 Answers

Your echo statement is incorrect, which is your problem. It should be:

public function display()
{
    echo $this->x;
}

Note that there's only one $ here: right before the keyword this. You mistakenly had two dollar signs.

like image 69
Jonah Bishop Avatar answered Dec 11 '22 11:12

Jonah Bishop