PHP类为什么使用public关键字? [英] PHP classes why use public keyword?

查看:131
本文介绍了PHP类为什么使用public关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要使用关键字 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关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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