问题描述
为什么要使用关键字 public
声明类属性(变量)或方法(函数),如果它们默认为public?还是他们?
为了区别我的问题, public
是否冗余?我理解 private
和 protected
,但为什么声明 public
if
是, public
是默认的(请参见)。
人们添加它,所以它与所有其他方法/属性一致。
此外,如果你想声明一个属性public并且不想使用 public
,你将需要使用 var
,这是不推荐使用的(好吧,它不会再通过错误,仍然不推荐。)
Why should I declare class properties (variables) or methods (functions) using the keyword public
, if they're public by default? Or, are they?
To phrase my question differently, is public
redundant? I understand private
and protected
, but why declare public
if class members are public anyway?
Yes, public
is the default (see visibility docs).
People add it, so it is consistent with all the other methods / properties.
Furthermore, if you want to declare a property public and don't want to use public
you will need to use var
, which is deprecated (well, it won't through an error anymore, but it is still unrecommended.)
这篇关于PHP类为什么使用public关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!