【发布时间】:2017-03-21 06:13:15
【问题描述】:
有没有办法证明某个类对另一个类中定义的每个方法都有神奇的方法?
我正在使用 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...
}
【问题讨论】:
-
我认为
$this->aInstance->{$name}(...$arguments);在 PHP 7.0 及更高版本中已被弃用。可能需要在那里使用call_user_func -
另外,为什么
B不能扩展A?轻松解决问题 -
B 已经有一个父母。
-
嗯,你试过类似这个问题的东西吗? stackoverflow.com/questions/15634021/…
-
通过 PHPDoc 执行此操作的唯一方法是通过
@method手动列出每个方法,如上一条评论中所述(将在支持它的其他编辑器/IDE 中工作)。虽然在 PhpStorm 中@mixin className也可以工作..这可能会为您完成这项工作。