偶然地,我注意到以下内容适用于PHP:
class
{
public function __construct()
{
// Notice that this was not declared before use
$this->not_declared = "Hello";
}
public function display()
{
echo $this->not_declared; // Outputs "Hello"
}
}
这是不好的做法吗?我总是认为必须在使用前声明属性,如下所示:
class
{
private $not_declared = "";
public function __construct()
{
// In this case, it was declared above
$this->not_declared = "Hello";
}
public function display()
{
echo $this->not_declared; // Outputs "Hello"
}
}