PHP 类实例化.使用还是不使用括号? [英] PHP class instantiation. To use or not to use the parentheses?

查看:28
本文介绍了PHP 类实例化.使用还是不使用括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直假设 - 在没有构造函数参数的情况下 - 创建类实例时,跟在类名后面的括号(大括号)是可选的,并且您可以根据自己的心血来潮包含或排除它们.

I've always assumed that - in the absence of constructor parameters - the parentheses (curly brackets) follow the class name when creating a class instance, were optional, and that you could include or exclude them at your own personal whim.

这两个陈述是相等的:

$foo = new bar;
$foo = new bar();

我说得对吗?或者我不知道的括号有什么意义?

Am I right? Or is there some significance to the brackets that I am unaware of?

我知道这听起来像是一个 RTM 问题,但我已经搜索了一段时间(包括整个 PHP OOP 部分),但似乎找不到直接的答案.

I know this sounds like a RTM question, but I've been searching for a while (including the entire PHP OOP section) and I can't seem to find a straight answer.

推荐答案

它们是等效的.如果您没有按照任何代码约定编码,请使用您更喜欢的.就我个人而言,我喜欢忽略它,因为它对我来说真的只是杂乱无章.

They are equivalent. If you are not coding by any code convention, use which you like better. Personally, I like to leave it out, as it is really just clutter to me.

这篇关于PHP 类实例化.使用还是不使用括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆