Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...
}
like image 364
Michael Avatar asked Nov 07 '16 19:11

Michael


1 Answers

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.

like image 140
LazyOne Avatar answered Oct 27 '22 08:10

LazyOne