douan3414 2010-01-28 06:50
浏览 109
已采纳

php函数调用中的下划线

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()."
" );
print( "Handle = ".DatabaseConnection::get()->handle()."
" );

in this code, what does it mean to have underscores in variables?

  • 写回答

4条回答 默认 最新

  • dongqian6234 2010-01-28 06:51
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀