Person93 Person93 - 25 days ago 11
PHP Question

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

Answer

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: http://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.

Comments