略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ReflectionClass::getInterfaceNames

2024-12-28

ReflectionClass::getInterfaceNames

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

ReflectionClass::getInterfaceNames获取接口(interface)名称

说明

public ReflectionClass::getInterfaceNames(): array

获取接口(interface)名称。

参数

此函数没有参数。

返回值

一个数值数组,接口(interface)的名称是数组的值。

范例

示例 #1 ReflectionClass::getInterfaceNames() 例子

<?php
interface Foo { }

interface 
Bar { }

class 
Baz implements FooBar { }

$rc1 = new ReflectionClass("Baz");

print_r($rc1->getInterfaceNames());
?>

以上例程的输出类似于:

Array
(
    [0] => Foo
    [1] => Bar
)

参见

add a noteadd a note

User Contributed Notes 2 notes

up
10
pinpin
11 years ago
It seems the interface names are actually listed in a defined order:
- "extends" takes precedence over "implements" (i.e. first will be interfaces from (implemented in) the parent class (if any), then interfaces implemented in the class itself)
- when multiple interfaces are implemented at one time/level, it can be:
+ from an "implements" : they're listed in the defined order
+ from an "extends" (a class extends another class which implements multiple interfaces; or an interface extends multiple interfaces) : they're listed in REVERSE order

<?php
interface Foo {}
interface
Bar {}
interface
Other {}
interface
Foobar extends Foo, Bar {}
interface
Barfoo extends Bar, Foo {}

class
Test1 implements Foo, Bar {}
class
Test2 implements Bar, Foo {}

class
Test3 extends Test1 {}
class
Test4 extends Test2 {}

class
Test5 extends Test1 implements Other {}

class
Test6 implements Foobar, Other {}
class
TestO implements Other {}
class
Test7 extends TestO implements Barfoo {}

$r=new ReflectionClass('Test1');
print_r($r->getInterfaceNames()); // Foo, Bar

$r=new ReflectionClass('Test2');
print_r($r->getInterfaceNames()); // Bar, Foo

$r=new ReflectionClass('Test3');
print_r($r->getInterfaceNames()); // Bar, Foo

$r=new ReflectionClass('Test4');
print_r($r->getInterfaceNames()); // Foo, Bar

$r=new ReflectionClass('Test5');
print_r($r->getInterfaceNames()); // Bar, Foo, Other

$r=new ReflectionClass('Test6');
print_r($r->getInterfaceNames()); // Foobar, Bar, Foo, Other

$r=new ReflectionClass('Test7');
print_r($r->getInterfaceNames()); // Other, Barfoo, Foo, Bar
?>
up
1
pvdhurk
1 year ago
For the record, it will return the full name (not the short name) for each Interface

Example with namespace would've helped.

官方地址:https://www.php.net/manual/en/reflectionclass.getinterfacenames.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3