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 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错