首页 > 解决方案 > 由数组组成的对象的 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 看到了这个,我不明白他们是怎么做到的。

标签: phpphpdoc

解决方案


您可以使用@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
{
}

推荐阅读