如何从另一种方法继承 phpdoc(不是我们要覆盖的方法)

Ali*_*Ali 4 php phpdoc

如果有一种方法可以在相同和/或不同类中的不同方法上复制(继承)完全相同的 phpdoc,我就在徘徊。

我知道内联 {@inheritdoc},它只提供从父类到其子类的继承(正如人们所期望的:)。

但是看看下面,我必须在 3 个不同的地方复制完全相同的描述,在同一个类中的 2 个方法和在单独的类中的一个(不是继承而是启动)。我想知道是否有一种方法可以做到这一点,而无需复制粘贴。

class Example
{
    /**
     * here i want to have exactly the same phpdoc,
     * like what i created for getMeSomethingFromTheShelf() in the same class
     */
    public function getMeSomething($id, $option = [])
    {
        return $this->getMeSomethingFromTheShelf($id, $option);
    }

    /**
     * Some description
     * and some more detail about how the $option value will be used
     *
     * @param int $id
     * @param array $option and here some we have some sample
     * @return string
     */
    public function getMeSomethingFromTheShelf($id, $option = [])
    {
    return 'here you go :)';
    }
}

class AnotherClass
{
    /**
     * here also i want to have exactly the same phpdoc,
     * like what i created for Example::getMeSomethingFromTheShelf() in the Example class
     */
    public function fetchSomething($id, $option = [])
    {
        return (new Example)->getMeSomething($id, $option = []);
    }
}
Run Code Online (Sandbox Code Playgroud)

感谢您的任何建议,提前:)

Mik*_*ike 6

{@inheritdoc}是内置的,不支持指定继承源。请使用@see标签。

不太可能以相同的方式描述两种方法。如果它是一个代理,或者只是传递数据,那么描述应该注意这种行为,并且@see标签应该将用户引导到接口方法。

我经常使用@see@link标签来链接到 SAPI 文档,或解析代理。也就是说,我总是尝试包含某种描述,并且总是记录参数和返回,即使它们是相同的(在查看代码时会有所帮助)。

小心不要过于依赖 PHPDoc 的快捷方式。