实例化带括号或不带括号的类? [英] Instantiate a class with or without parentheses?

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

问题描述

可能重复:
PHP类实例化.使用还是不使用括号?

Possible Duplicate:
PHP class instantiation. To use or not to use the parentheses?

我还没有找到任何官方文档.但是afaik,是否在没有括号的情况下使用我们的类实例化并不重要-只要不涉及参数,对吧?

I have not found any official documentation on this. But afaik it doesn't matter whether a class is instantiated with our without the parentheses - as long as there are no parameters involved, right?

$car = new Car;

$car = new Car();

但是有人可以告诉我性能是否有所不同? 更正确"的方法是哪一种?有任何官方文件吗?

But can anyone tell me if there's a difference in performance? Which way is the 'more correct' way? Is there any official documentation for this?

推荐答案

任何性能上的差异都是可以忽略的.

Any difference in performance is going to be absolutely negligible.

虽然两种方法都很好,但我个人更喜欢使用new Car();,因为通常在这里调用方法 ,而PHP中的函数/方法调用需要().而且,它与带有参数的实例更一致.

While both ways are fine, I personally would prefer using new Car(); because usually, a method is being called here, and function/method calls in PHP require (). Also, it's more consistent with instantiations that have parameters.

但是最终,它的味道下降了.选择哪种方式都没有关系,但是当您选择一种方式时,请始终坚持下去!

But in the end, it's down to taste. It doesn't matter which way you choose, but when you choose one, stick to it consistently!

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

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