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_implements
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
class_implements
Референца за `function.class-implements.php` со подобрена типографија и навигација.
class_implements
класата mysqli_driver
class_implements — Врати ги интерфејсите што ги имплементира дадената класа или интерфејс
= NULL
Оваа функција враќа низа со имињата на интерфејсите што дадениот object_or_class и неговите родители ги имплементираат.
Параметри
object_or_class-
Објект (инстанца на класа) или стринг (име на класа или интерфејс).
autoload-
Дали да autoload ако веќе не е вчитан.
Вратени вредности
Низа при успех, или false кога дадената класа не постои.
Примери
Пример #1 class_implements() example
<?php
interface foo { }
class bar implements foo {}
print_r(class_implements(new bar));
// you may also specify the parameter as a string
print_r(class_implements('bar'));
spl_autoload_register();
// use autoloading to load the 'not_loaded' class
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() треба да се користи наместо тоа.
Види Исто така
- class_parents() - Врати ги родителските класи на дадената класа
- get_declared_interfaces() вратената вредност.
- is_a() - Проверува дали објектот е од даден тип или подтип
instanceof
Белешки од корисници 3 белешки
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.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
)