PHP的获取接口方法 [英] php get interface methods

查看:102
本文介绍了PHP的获取接口方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取已实现接口的方法?

Is it possible to get the methods of an implemented interface?

例如,仅返回接口中的功能bar()。

For example, to return only function bar() that is in interface.

interface iFoo  
{   
  public function bar(); 
}

class Foo implements iFoo 
{   
  public function bar()
  { 
    ...
  }

  public function fooBar()
  {
    ...
  }
}


我知道我可以使用class_implements返回已实现的接口,例如


I know I can use class_implements to return the implemented interfaces, for example

print_r(class_implements('Foo'));

output:
Array ( [iFoo] => iFoo ) 

如何获取已实现接口的方法?

How do I get the methods of the implemented interfaces?

推荐答案

通过定义,实现接口意味着您必须定义子类中的 ALL 个方法,因此您要查找的是接口中方法的 ALL 个。

By definition, implementing an interface means that you must define ALL methods in the child class, so what you are looking for is ALL of the methods from the interface(s).

单个界面:

$interface = class_implements('Foo');
$methods_implemented = get_class_methods(array_shift($interface));
var_dump($methods_implemented);

输出:

array (size=1)
  0 => string 'bar' (length=3)

多个接口:

$interfaces = class_implements('Foo');

$methods_implemented = array();
foreach($interfaces as $interface) {
    $methods_implemented = array_merge($methods_implemented, get_class_methods($interface));
}
var_dump($methods_implemented);

输出:

array (size=2)
  0 => string 'bar' (length=3)
  1 => string 'ubar' (length=4)

添加的接口 uFoo 到您的示例:

interface uFoo {
    public function ubar();
}

interface iFoo  
{   
  public function bar(); 
}

class Foo implements iFoo, uFoo
{   
  public function bar()
  { 
  }

  public function fooBar()
  {
  }
  public function ubar(){}
}

这篇关于PHP的获取接口方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆