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

Is there a way to indicate that a class has magic methods defined for every method on another class?

有没有办法证明某个 class 对另一个 class 中定义的每个方法都有魔术方法?

我正在使用 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...
}

正确的解决方案是使用受支持的 @method PHPDoc 标签。这样它也可以在其他支持 PHPDoc 并理解此类标准标签的 editors/IDEs 中使用。

这种方法需要单独列出每个方法。更多关于这个在另一个 Whosebug question/answer: .


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

在目标 PHP 的文档注释中添加 @mixing className class 应该可以完成这项工作。

/**
 * Class B
 * 
 * @mixin A
 */
class B
{

基本上,@mixin 标签的作用与实际 PHP 的特征相同。

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