开发者

Importance of protected/private in PHP classes

开发者 https://www.devze.com 2022-12-18 01:18 出处:网络
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 wou开发者_如何学运维ld n

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 wou开发者_如何学运维ld 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.


It's not really about security, but more about the OOP design. They tell you, and other developers, how the class should be used. Private and protected variables are for internal use, while public members are for public use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号