有没有办法记录某个类对另一个类中定义的每个方法都有魔术方法?
我正在使用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的实际特性.
请注意,不能保证在将来的某个时候不会删除对此类标记的支持,尽管这种可能性不大.