有没有办法表明一个类为另一个类上的每个方法定义了魔术方法? [英] Is there a way to indicate that a class has magic methods defined for every method on another class?

查看:91
本文介绍了有没有办法表明一个类为另一个类上的每个方法定义了魔术方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以证明某个类对另一个类中定义的每个方法都具有魔术方法?

我正在使用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并了解这种标准标记的编辑器/IDE中使用.

此方法要求每种方法分别列出.有关此问题的更多信息,请参见另一个StackOverflow问题/答案: https://stackoverflow.com/a/15634488/783119 .. >


在当前的PhpStorm版本中,您可以使用非PHPDoc规范(因此可能是PhpStorm特定的)@mixin标记.

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

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

基本上,@mixin标记可以执行实际PHP的特征.

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

Is there a way to document that a certain class has magic methods for every method defined in another class?

I am using PhpStorm, so I would be happy with any solution that will get autocomplete to work properly for that.

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...
}

解决方案

The proper solution is to use supported @method PHPDoc tags. This way it will also work in other editors/IDEs that support PHPDoc and understand such standard tag.

This approach requires every method to be listed separately. More on this in another StackOverflow question/answer: https://stackoverflow.com/a/15634488/783119.


In current PhpStorm versions you may use not-in-PHPDoc-specs (and therefore possibly PhpStorm-specific) @mixin tag.

Adding @mixing className in PHPDoc comment for your target class should do the job for you.

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

Basically, @mixin tag does what actual PHP's traits do.

Please note that there is no guarantee that support for such tag will not be removed at some point in the future, although it's pretty unlikely.

这篇关于有没有办法表明一个类为另一个类上的每个方法定义了魔术方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆