有没有办法表明一个类为另一个类的每个方法定义了魔术方法?

Per*_*n93 6 php phpdoc phpstorm

有没有办法记录某个类对另一个类中定义的每个方法都有魔术方法?

我正在使用 PhpStorm,所以我会对任何可以让自动完成功能正常工作的解决方案感到满意。

class A
{
    // a bunch of functions go here...
}

/**
 * Class B
 * What should go here to make it work???
 */
class B
{
    private $aInstance;

public function __construct() {
    $this->aInstance = new A();
}

public function __call($name, $arguments) {
    // TODO: Implement __call() method.
    if(method_exists($this->aInstance, $name)) {
        return $this->aInstance->{$name}(...$arguments);
    }
    throw new BadMethodCallException();
}

    // a bunch more functions go here...
}
Run Code Online (Sandbox Code Playgroud)

Laz*_*One 6

正确的解决方案是使用受支持的@methodPHPDoc 标签。这样,它也可以在支持 PHPDoc 并理解此类标准标签的其他编辑器/IDE 中工作。

这种方法需要单独列出每种方法。在另一个 StackOverflow 问题/答案中详细介绍了这一点:https : //stackoverflow.com/a/15634488/783119


在当前的 PhpStorm 版本中,您可以使用 not-in-PHPDoc-specs(因此可能是 PhpStorm 特定的)@mixin标签。

@mixing className为您的目标类添加PHPDoc 注释应该可以为您完成这项工作。

/**
 * Class B
 * 
 * @mixin A
 */
class B
{
Run Code Online (Sandbox Code Playgroud)

基本上,@mixin标签所做的就是实际的 PHP 特性。

请注意,虽然不太可能,但不能保证在将来的某个时候不会删除对此类标签的支持。