Class Constructor Calls
When calling class constructors with no arguments, always include parentheses:
$foo = new MyClassName();
This is to maintain consistency with constructors that have arguments:
$foo = new MyClassName($arg1, $arg2);
Note :
If the class name is a variable, the variable will be evaluated first to get the class name, and then the
constructor will be called.
Use the same syntax:
$bar = ‘MyClassName’;
$foo = new $bar();
$foo = new $bar($arg1, $arg2);
Class Constructor CallsWhen calling class constructors with no arguments, always include parentheses:$foo = new MyClassName();
This is to maintain consistency with constructors that have arguments:$foo = new MyClassName($arg1, $arg2);
Note that if the class name is a variable, the variable will be evaluated first to get the class name, and then the
constructor will be called. Use the same syntax:$bar = ‘MyClassName’;$foo = new $bar();$foo = new $bar($arg1, $arg2);
This is to maintain consistency with constructors that have arguments:$foo = new MyClassName($arg1, $arg2);
Note that if the class name is a variable, the variable will be evaluated first to get the class name, and then the
constructor will be called. Use the same syntax:$bar = ‘MyClassName’;$foo = new $bar();$foo = new $bar($arg1, $arg2);
Comments