downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

class_parents> <SPL 関数
Last updated: Fri, 06 Nov 2009

view this page in

class_implements

(PHP 5 >= 5.1.0)

class_implements 与えられたクラスが実装しているインターフェースを返す

説明

array class_implements ( mixed $class [, bool $autoload = true ] )

この関数は、与えられたクラス 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_parents> <SPL 関数
Last updated: Fri, 06 Nov 2009
 
add a note add a note User Contributed Notes
class_implements
24-Jan-2006 05:51
You can use Reflection class instead:

<?php

$r
= new ReflectionClass($class);
foreach(
$r->getInterfaces() as $in)
{
 
$in->getName();
}
trollll23 at yahoo dot com
25-Oct-2005 07: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
01-Aug-2005 07: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.

class_parents> <SPL 関数
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites