You can use Reflection class instead:
<?php
$r = new ReflectionClass($class);
foreach($r->getInterfaces() as $in)
{
$in->getName();
}
class_implements
(PHP 5)
class_implements — 与えられたクラスが実装しているインターフェースを返す
説明
この関数は、与えられたクラス class とその親が実装しているインターフェースを配列で返します。
パラメータ
- class
-
オブジェクト (クラスインターフェース) もしくは文字列 (クラス名) を指定します。
- autoload
-
__autoload マジックメソッドを通じて、 この関数にクラスを自動的にロードさせるかどうかを指定します。 デフォルトは TRUE です。
返り値
成功した場合に配列、エラー時に FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.1.0 | 文字列として class パラメータを渡すオプションが追加されました。 autoload パラメータが追加されました。 |
例
例1 class_implements() の例
<?php
interface foo { }
class bar implements foo {}
print_r(class_implements(new bar));
// PHP 5.1.0 以降、パラメータを文字列として指定しても良い
print_r(class_implements('bar'));
function __autoload($class_name) {
require_once $class_name . '.php';
}
// 'not_loaded' クラスをロードするために __autoload を使用する
print_r(class_implements('not_loaded', true));
?>
上の例の出力は、たとえば 以下のようになります。
Array ( [foo] => foo ) Array ( [interface_of_not_loaded] => interface_of_not_loaded )
class_implements
25-Jan-2006 02:51
trollll23 at yahoo dot com
26-Oct-2005 04:57
26-Oct-2005 04:57
Luckily, it prints out superinterfaces as well in reverse order so iterative searching works fine:
<?php
interface InterfaceA { }
interface InterfaceB extends InterfaceA { }
class MyClass implements InterfaceB { }
print_r(class_implements(new MyClass()));
?>
prints out:
Array
(
[InterfaceB] => InterfaceB
[InterfaceA] => InterfaceA
)
ludvig dot ericson at gmail dot nospam dot com
02-Aug-2005 04:41
02-Aug-2005 04:41
Hint:
<?php
in_array("your-interface", class_implements($object_or_class_name));
?>
would check if 'your-interface' is ONE of the implemented interfaces.
Note that you can use something similar to be sure the class only implements that, (whyever you would want that?)
<?php
array("your-interface") == class_implements($object_or_class_name);
?>
I use the first technique to check if a module has the correct interface implemented, or else it throws an exception.
