括号的目的是输入构造函数可以接受的任何参数.
class Example{
private $str;
public function __construct($str){
$this->str = $str;
}
public function output(){
echo $this->str;
}
}
$ex = new Example; // missing argument error
$ex = new Example('Something');
$ex->output(); // echos "Something"
Run Code Online (Sandbox Code Playgroud)
如果您的类构造函数不接受任何参数,您可以将括号括起来.为了良好的代码,我总是保留括号,无论构造函数是否接受任何参数.
大多数来自C#或Java背景的程序员都会保留括号,因为它更熟悉它们.