为什么我们在PHP中声明一些变量就像"$ _variablename"......?

Bak*_*Raj 6 php oop variables

为什么我们在PHP中声明一些变量$_variablename

是否_界定什么?

请帮我澄清一下,谢谢.

Ben*_*aum 8

这是一个命名惯例.

关于命名约定的梨手册:

私有类成员前面有一个下划线.例如:

$_status
Run Code Online (Sandbox Code Playgroud)


Sub*_*axe 6

下划线并不意味着该变量是私有的。没有必要使用下划线。它只是一个命名约定,提醒开发人员变量/属性/方法是私有的还是受保护的

例如

// 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() {}  
}  
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,下划线不是使变量或方法私有的原因;private/protected 关键字就是这样做的。