Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

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

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...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...