php - 由数组组成的对象的 PHPDoc
问题描述
我有具有以下规则的 config.php 文件:
return [
'debug' => true,
'db_debug' => true,
'lang' => 'Eng',
...
等等。当我的应用程序初始化时,该文件将在类的构造函数中使用,该类的构造函数会将这个数组转换为其属性:
class Config
{
public function __construct($config) {
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
}
如何在使用这样的配置值时为其属性编写 PHPDoc 以查看 PHPStorm 中的建议(当我键入$config->
IDE 时会建议配置键):
$config = new Config();
echo $config->lang //Eng
我在 Yii 看到了这个,我不明白他们是怎么做到的。
解决方案
您可以使用@property
类级别块中的注释来列出属性。即使没有明确定义,您的 IDE 也应该自动完成。
/**
* @property int $id The ID of the thing
* @property string $name The name of the thing.
* @property bool $debug True to enable debugging.
*/
class Config
{
}
推荐阅读
- nginx - 如何在 URL 末尾的正斜杠之后修复 nginx 中的重定向
- ruby - 如何在安装 Ruby 时使用 SHA?如果我不验证它们会有什么风险?
- scala - 无法找到或加载主类 com.weekdays.claobj.Ann1
- https - 在 docker 容器中添加信任证书
- excel - 如何检查 Excel 用户窗体上的所有文本框是否都已填充?
- popup - 无法关闭附加到关闭按钮的 EventListener 的动态创建的弹出窗口?
- assembly - 如何在装配中添加?
- ios - UITabBar Lifecycle 的方法不会从后台启动
- docker - 使用官方 Microsoft .Net Core SDK 映像以交互模式使用 Docker
- linux - 为什么从 x86_64 汇编函数调用 C abort() 函数会导致分段错误 (SIGSEGV) 而不是中止信号?