class_implements

(PHP 5 >= 5.1.0)

class_implements Возвращает список интерфейсов, реализованных в заданном классе

Описание

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

Функция возвращает массив имен интерфейсов, реализованных в заданном классе class и его родительских классах.

Список параметров

class

Объект (экземпляр класса) или строка (имя класса).

autoload

В зависимости от переданного значения фунция может загрузить описание класса автоматически магическим методом __autoload().

Возвращаемые значения

В случае успеха будет возвращен массив. В случае ошибки - 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';
}

// использование __autoload для загрузки еще незагруженного класса 'not_loaded'
print_r(class_implements('not_loaded'true));

?>

Результатом выполнения данного примера будет что-то подобное:

Array
(
    [foo] => foo
)

Array
(
    [interface_of_not_loaded] => interface_of_not_loaded
)

Смотрите также

  • class_parents() - Возвращает список родительских классов заданного класса
  • get_declared_interfaces() - Возвращает массив всех объявленных интерфейсов

Коментарии

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.
2005-08-01 15:41:55
http://php5.kiev.ua/manual/ru/function.class-implements.html
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
    )
2005-10-25 15:57:08
http://php5.kiev.ua/manual/ru/function.class-implements.html
Calling class_implements with a non-loadable class name or a non-object results in a warning:

<?php
// Warning: class_implements(): Class abc does not exist and could not be loaded in /home/a.panek/Projects/sauce/lib/Sauce/functions.php on line 196

$interfaces class_implements('abc');
?>

This is not documented and should just return FALSE as the documentation above says.
2013-11-05 15:29:28
http://php5.kiev.ua/manual/ru/function.class-implements.html
The order of interfaces is not reliable and varies between PHP versions.
2019-04-03 22:16:47
http://php5.kiev.ua/manual/ru/function.class-implements.html

    Поддержать сайт на родительском проекте КГБ