« SPL 関数
class_parents »

class_implements

(PHP 5, PHP 7, PHP 8)

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

説明

function class_implements(object|string $object_or_class, bool $autoload = true): array|false

この関数は、与えられたクラス object_or_class とその親が実装しているインターフェイスを配列で返します。

パラメータ

object_or_class

オブジェクト (クラスのインスタンス) もしくは文字列 (クラス名あるいはインターフェイス名) を指定します。

autoload

まだロードされていない場合に オートロード するかどうか。

戻り値

成功した場合に配列、 指定されたクラスが存在しない場合に false を返します。

例

例1 class_implements() の例

<?php

interface foo { }
class bar implements foo {}

print_r(class_implements(new bar));

// パラメータを文字列として指定しても良い
print_r(class_implements('bar'));

spl_autoload_register();

// 'not_loaded' クラスをロードするためにオートローディングを使用する
print_r(class_implements('not_loaded', true));

?>

上の例の出力は、 たとえば以下のようになります。

Array
(
    [foo] => foo
)
Array
(
    [foo] => foo
)
Array
(
    [interface_of_not_loaded] => interface_of_not_loaded
)

注意

注意:

オブジェクトが、インターフェイスを実装しているかどうかを調べるには、 instanceof か、is_a() 関数を代わりに使うべきです。

参考

関連キーワード:
インターフェイス   implements   実装   loaded   autoload   ロード   interface   print   parents   オート   instanceof   インスタンス   成功   register   ローディング   参考   declared   interfaces   宣言
関連する関数・ページ:
function.class-parents   function.interface-exists   language.oop5.interfaces   class.traversable   spl.interfaces   function.get-declared-interfaces