如何解释 PHPDoc 的 `@property-read` 和 `@property-write` 属性?
Given the phpDoc manual, I cannot find explanation about that
@property-read
@property-write
but only of @property
.
What do they do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@property 标签是 phpDocumentor 手册中记录的内容。该标签仅在类文档块中用于记录“神奇”属性/变量,即未在代码中定义的属性/变量,因此不能被“文档块化”。
现在,当您想强调特定的“魔术变量”是只读的(不可写)时,您可以使用@property-read。如果您有一个只写(不可读)的“神奇变量”,您可以使用@property-write。诚然,我很难想象无法读取的只写变量,但由于技术上可以做到这一点,@property-write 可供您记录它。
@property 的这两个“子标签”在您上面链接到的 @property 页面上进行了进一步解释。
The @property tag is what's documented in the phpDocumentor manual. This tag is used only in a class docblock to document a "magic" property/variable, i.e. one that is not defined in the code, and therefore can't be "docblock'd".
Now, when you want to highlight that a particular "magic variable" is read-only (not writable), you'd use @property-read. If you have a "magic variable" that is write-only (not readable), you'd use @property-write. Granted, I have trouble imagining write-only variables that can't be read, but since it's technically possible to do it, @property-write is available for you to document it.
These two "subtags" of @property are explained farther down on the @property page that you linked to above.
这里有一个关于魔法属性的很好的解释。
基本上,
@property-write
被解释为 - 顾名思义 - 为只写属性。例如,Eclipse 中的代码完成就利用了这一点。如果您的魔法属性foo
被声明为“只写”,那么当您键入$a = $this->f
时,它不会显示在代码完成中。Here's a good explanation on magic properties.
Basically,
@property-write
is interpreted - as the name suggests - as a write-only property. The code completion in Eclipse, for example, makes use of this. If your magic propertyfoo
is declared "write-only", it wouldn't show up in code completion, when you type$a = $this->f
.@property-read
告诉 IDE(和文档生成器)存在一个神奇的仅用于 Get 的字段。@property-write
告诉 IDE 存在一个神奇的 Set-only 字段。PHP 8.1 添加了
readonly
关键字,它基本上禁止将变量设置为另一个值(初始值除外)。
@property-read
tells the IDE (and doc generator) that a magic Get-only field exists.@property-write
tells the IDE that a magic Set-only field exists.PHP 8.1 added
readonly
keyword,which basically forbids setting a variable to another value (except initial value).