2

What is $_var in PHP? Why is it used like this?

class User 
{
     private $_name;

     public function __construct() 
     {
          $this->_name = "Joseph Crawford Jr.";
     }

     public function GetName() 
     {
          return $this->_name;
     }
}
Jimbo
  • 25,790
  • 15
  • 86
  • 131
cvas
  • 31
  • 1
  • 4

3 Answers3

6

Some developers like to prefix variables with underscores to show that they are private or protected class variables.

This isn't anything big, it's just opinion. If you want to stick to some 'standards', check out PSR.

Jimbo
  • 25,790
  • 15
  • 86
  • 131
0

It just starts with an underscore to visually distinguish that it's a class field in this case. There is nothing special with such variables.

Joey
  • 344,408
  • 85
  • 689
  • 683
0

It's an older-style way of signifying a variable as protected or private in PHP4 when there was no differentiation between public, protected and private like there is in PHP5.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49