PHP类中受保护/私有的重要性 [英] Importance of protected/private in PHP classes

查看:57
本文介绍了PHP类中受保护/私有的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当我看到PHP类时,大多数变量和函数都是私有的或受保护的.因为PHP是在服务器端执行的,所以我不明白您为什么需要这些安全功能.这些可用于安全性还是我缺少什么?

Usually when I see PHP classes, most of the variables and functions are either private or protected. Because the PHP is executed on the server side, I don't understand why you would need these security features. Are these available for security or am I missing something?

推荐答案

Private and protected make sense in the context of visibility, and not security features stricto senso.

被宣布为公开的班级成员可以是 随处访问.成员宣布 受保护的只能在内部访问 类本身,并通过继承和 家长班.宣布为 私有只能由 定义成员的类.

Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

这篇关于PHP类中受保护/私有的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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