dongzhan3937 2017-08-18 12:20
浏览 55
已采纳

为什么php将空字节添加到私有和受保护的属性名称?

I am new to PHP world and learning it from php.net. I know that when casting object to an array then the null byte is added around the private and protected property names when ClassName or asterisk key (*) is prepended to the private and protected property names in the array keys.

But my question is that WHY php add null bytes WHAT is the reason ?

Can anyone tell in simple and easy words.

Examples will help a lot.

Thanks

  • 写回答

1条回答 默认 最新

  • doucan4873 2017-08-18 12:40
    关注

    The point of private/protected properties is that you're not supposed to access them from outside the class itself. This is not a security measure or anything like that, it's to enforce contracts between different pieces of your code. When you mark something as private/protected, you're declaring explicitly that this thing isn't for general public consumption and no external code should be coupled to it.

    This is mostly a reminder for yourself and other developers and will at worst give you light slap on the wrist if you disobey that marker; it's not an ironclad protection by any means. There are any number of ways around that, e.g. using Reflection. But, if it was made too easy to access those private parts, developers would probably be doing it left and right and negate the entire point.

    Since those properties are included in the array when casting an object to an array, at the very least it's not immediately obvious how to access them directly due to the added NUL bytes. If you take the time to figure out how to access them, you hopefully really know what you're doing.

    TL;DR: (I believe) it's a minimum attempt to try to enforce some minimal coding standards and not let newbies violate all OOP principles once they figure out what an array cast is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?