php – 有没有办法表明一个类是否为另一个类的每个方法定义了魔
发布时间:2020-12-13 13:45:24 所属栏目:PHP教程 来源:网络整理
导读:有没有办法记录某个类对另一个类中定义的每个方法都有魔术方法? 我正在使用PhpStorm,所以我会对任何可以自动完成正常工作的解决方案感到满意. class A{ // a bunch of functions go here...}/** * Class B * What should go here to make it work??? */clas
有没有办法记录某个类对另一个类中定义的每个方法都有魔术方法?
我正在使用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的实际特性. 请注意,不能保证在将来的某个时候不会删除对此类标记的支持,尽管这种可能性不大. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |