getter 是防止公共属性发生更改的唯一方法吗?
Possible Duplicate:
PHP Readonly Properties?
I want a property to be publicly visible:
echo $object->prop;
but I don't want to be modifiable.
Are there other options to make this happen, besides using __get
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否,但有功能请求:https://bugs.php.net/bug .php?id=46506 所以你可以投票支持它的重要性,并希望这个问题能在不久的将来得到解决。
但我认为这不会在不久的将来,因为这个功能会造成很多混乱,就像存储在属性中的对象一样 - 它们只是对对象的引用,因此使它们可见可以让您对其方法/属性进行操作。
就目前而言,为每个您想要只读的属性创建适当的 getter 似乎是最好的解决方案。
No, but there is feature request for it: https://bugs.php.net/bug.php?id=46506 so you can vote for importance of it and hope that this will be resolved in near future.
But I don't think it will be near future since this feature will create a lot of confusion, like objects stored in properties - they are only references to the object, so making them visible allows you to operate on it's methods / properties.
As for now, creating proper getter for every property, that you want to be read-only, seems to be the best solution.
我能想到的其他方法:
__set()
函数并忽略设置。希望这有帮助:)
Other ways I can think of:
__set()
function and ignore the setting.Hope this helps :)