如何在 PHPDoc 中标记参数是可选的?

发布于 2024-08-16 16:14:11 字数 428 浏览 1 评论 0原文

我有这个带有可选参数的构造函数。这样做的主要问题是可用性。使用我的框架的开发人员会立即感到头疼,因为他不知道他是否可以提供一个论点,什么样的论点,或者他是否根本不能。结论:这很糟糕。但是如果有人安装了像 Netbeans 这样的合理 IDE,PHPDoc 可能会有所帮助;)

那么:

class ChildClass extends ParentClass {
    public function __construct() {
    $tplFile = func_get_arg(0);
    if (!isset($tpl)) {
        $tpl = 'index';
    }
    parent::__construct($tpl);
    }
}

我如何在这里使用 PHPDoc 来指示可以提供可选的 [$tpl] 参数?

I've got this constructor that takes an optional argument. The main problem with this is usability. The developer using my framework will catch a headache instantly because he doesn't know if he can provide an argument, what kind of argument, or if he can't at all. Conclusion: It just sucks. But PHPDoc may help a little bit if someone has a reasonable IDE like Netbeans installed ;)

So:

class ChildClass extends ParentClass {
    public function __construct() {
    $tplFile = func_get_arg(0);
    if (!isset($tpl)) {
        $tpl = 'index';
    }
    parent::__construct($tpl);
    }
}

How could I use PHPDoc here to indicate that there can be provided an optional [$tpl] argument?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扭转时空 2024-08-23 16:14:11

声明一个参数并给它一个预设:

public function __construct($my_argument = 0) 

我的 IDE(phpEd,对 phpDoc 敏感)可以正确解释它。 phpDoc 也应该将参数放入大括号中:

show ([$my_argument])

Declare a parameter and give it a preset:

public function __construct($my_argument = 0) 

my IDE (phpEd, which is phpDoc sensitive) interprets it correctly. phpDoc should, too, and put the parameter into braces:

show ([$my_argument])
层林尽染 2024-08-23 16:14:11

您也可以使用 反射类 来实现此目的。

You can use reflection class for this too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文