This question already has answers here:
Closed 8 years ago.
Possible Duplicate:
PHP class instantiation. To use or not to use the parenthesis?
Omission of brackets and parameter-less object constructors
With or without the brackets, the new Class seems not bother. So, I doubt what's the usage of the brackets ()
. I searched php manual, didn't get it. Could anybody explain?
The purpose of the brackets is for you to enter any arguments that your constructor may accept.
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"
If your class constructor does not accept any arguments, you may leave the brackets out. For good code sake, I always keep the brackets, whether or not the constructor accepts any argument.
Most coders coming from C# or Java background would keep the parenthesis as it is more familar to them.