【问题标题】:PHPDoc for object made from array由数组组成的对象的 PHPDoc
【发布时间】:2021-08-05 14:31:43
【问题描述】:

我有以下规则的 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 看到了这个,我不明白他们是怎么做到的。

【问题讨论】:

    标签: php phpdoc


    【解决方案1】:

    您可以使用类级别块中的@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
    {
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-21
      • 2013-11-14
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      相关资源
      最近更新 更多