无法设置只读属性
PHP 中是否确实存在只读属性而我不知道它们?如何使公共财产变为只读?
我只是在玩 ReflectionClass,在尝试覆盖属性时收到此错误消息:
$lol = new ReflectionObject($obj);
$lol->name = 'awawawawa';
Are there actually read-only properties in PHP and I'm not aware of them? How do I get a public property to be read-only??
I was just playing with ReflectionClass and I got this error message when trying to overwrite a property:
$lol = new ReflectionObject($obj);
$lol->name = 'awawawawa';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从反射类的代码来看:
基本上,它明确禁止“name”和“class”属性。
但我找不到任何表明类属性存在的迹象!
From the code of the reflection class:
so basically, it's explicitly forbidding it for the "name" and "class" properties.
I can't find any indication that a class property exists though!
来自 ReflectionObject 的 PHP 手册页:
http://sk.php.net/manual /en/class.reflectionobject.php
关于他们如何执行此操作的内容不多,但我的猜测是它明确地查找 name 属性的写入并阻止它避免发生,因为这会使反射成为 说谎。
你可以自己做类似的事情:
From the PHP Manual Page for ReflectionObject:
http://sk.php.net/manual/en/class.reflectionobject.php
There isn't much on how they do this, but my guess would be that it's explicitly looking out for the writing of the name property and stopping it from happening as it would make the reflection a lie.
You could do something similar yourself:
PHP 文档:
不过,我在
Properties
页面中没有看到任何有关将内容设为只读的内容...我想可能会在它们前面加上final
前缀,但我不知道是否可以这是允许的,因为它只在方法中提到。PHP docs:
I don't see anything in the
Properties
page about making things read-only, though... I guess possibly prefix them withfinal
, but I don't know if that's allowed since it's only mentioned on methods.来自 ReflectionClass 的文档:
然而,文档还说,
这里需要注意的是,(即使文档看起来 ReflectionClass 是用纯 PHP 实现的)ReflectionClass 是 PHP 核心的一部分,因此是用 C 实现的!
尽管该财产记录为普通公共财产,但事实上并非如此!
我懒得去深入研究 PHP 源代码,但你会发现那里有一个特殊的情况处理,它保护公共财产,使其只读。编辑:参见马克·贝克斯的回答。
From the docs of ReflectionClass:
However, the docs also say
It is important to note here, that (even if the documentation looks ReflectionClass is implemented in pure PHP) the ReflectionClass is part of the PHP core, thus implemented in C!
Although the property is documented as being a normal public property, in fact its not!
I'm too lazy to dig into the PHP source code for this, but you will find a special case handling there which protects the public property making it read-only. EDIT: see Mark Bakers answer.