I know that underscores in function names in PHP is used to “implicitly” denote that they are supposed to be private…but I just saw this code:
class DatabaseConnection
{
public static function get()
{
static $db = null;
if ( $db == null )
$db = new DatabaseConnection();
return $db;
}
private $_handle = null;
private function __construct()
{
$dsn = 'mysql://root:password@localhost/photos';
$this->_handle =& DB::Connect( $dsn, array() );
}
public function handle()
{
return $this->_handle;
}
}
print( "Handle = ".DatabaseConnection::get()->handle()."\n" );
print( "Handle = ".DatabaseConnection::get()->handle()."\n" );
in this code, what does it mean to have underscores in variables?
It’s kind of the same for methods and properties : the convention is the same : having a name that starts by one underscore generally means they are to be considered as
private/protected.(Of course, it’s not the same with methods which have a name that starts by two underscore : those are magic methods, and two underscore should not be used for your “normal” method names)