Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we declare some variables in PHP as like "$_variablename"...? [duplicate]

Tags:

variables

oop

php

Why we declare some variables in PHP as like $_variablename?

Does _ define something?

Please help me to clear this up, thanks.

like image 289
Bakiya Raj Avatar asked Apr 09 '13 06:04

Bakiya Raj


1 Answers

The underscore does not mean that the variable is private. It is not necessary to use the underscore. It’s simply a naming convention that reminds the developer that the variable/property/method is either private or protected

For Example

// This variable is not available outside of the class  
private $_someVariable;  

class MyClass {  
   // This method is only available from within this class, or  
   // any others that inherit from it.   
   protected function __myFunction() {}  
}  

In the code above, the underscore is not what makes the variable or method private; the private/protected keyword does that.

like image 77
Subodh Ghulaxe Avatar answered Nov 15 '22 17:11

Subodh Ghulaxe