Home > Article > Backend Development > Is there a difference between `new bar` and `new bar()` in PHP class instantiation?
Optional Parentheses in PHP Class Instantiation
In PHP, when instantiating a class that does not require constructor parameters, developers often question the significance of including or excluding parentheses after the class name. The question arises: "Are these two statements equivalent?"
$foo = new bar; $foo = new bar();
The answer is yes. Both statements create an instance of the bar class.
Traditionally, parentheses were included to help clarify the purpose of the line. However, with the advent of modern code editors and IDEs, the use of parentheses has become less necessary.
The choice of whether or not to use parentheses is generally a matter of personal preference. Some developers prefer to include them for clarity, while others see them as unnecessary clutter. However, if you are adhering to a particular coding convention, it is important to follow the guidelines set forth by that convention.
In the absence of a specific coding convention, the recommendation is to use whichever style you find more readable and consistent with your codebase.
The above is the detailed content of Is there a difference between `new bar` and `new bar()` in PHP class instantiation?. For more information, please follow other related articles on the PHP Chinese website!