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

时间:2016-11-07 19:26:00

标签: php phpstorm phpdoc

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

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

1 个答案:

答案 0 :(得分:3)

正确的解决方案是使用支持的@method PHPDoc标记。这样它也可以在支持PHPDoc的其他编辑器/ IDE中使用并理解这种标准标记。

此方法要求单独列出每种方法。更多关于此问题的另一个StackOverflow问题/答案:https://stackoverflow.com/a/15634488/783119

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

在目标类的PHPDoc注释中添加@mixing className应该为您完成工作。

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

基本上,@mixin标记执行PHP的实际特性。

请注意,无法保证在将来的某个时间点不会删除对此类代码的支持,但这种情况不太可能。