douji8017 2013-09-08 21:41
浏览 67
已采纳

如何使用PHP OO保护私有类字段? [重复]

This question already has an answer here:

How to secure private class field with PHP OO? I don't want to make private class field $a public, but when I do something like that:

class Hidden {
   private $a = 'Very important, hidden variable';
   protected $b = 'secret';
}
$obj = new Hidden();
$c = (array)$obj;
print_r($c);

$a is available for everybody:

Array ( [Hiddena] => Very important, hidden variable [*b] => secret ) 
</div>
  • 写回答

1条回答 默认 最新

  • dou4381 2013-09-08 21:57
    关注

    Perhaps you could elaborate on your concerns and give an example of a scenario where an attacker could exploit this condition in your specific case, since if anybody is able to inject PHP code into your application, you have a big problem anyway.

    Access modifiers in OOP are about encapsulation, not actual security. With private, you prevent other developers from unintentionally accessing the fields, not malicious code or potential attackers.

    For this reason, SecureString was introduced in C#. To emulate something like this in PHP, you'd have to store an encrypted value in $a and keep the key in a "secure" location which nobody who has the power to execute print_r or var_dump can access (file, DB). But then again, anybody who can print_r your object, can probably do file_get_contents on the key file.

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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)