라이브러리
[PHP] ReflectionClass::getInterfaceNames - 인터페이스 이름을 가져옵니다.
ReflectionClass::getInterfaceNames()
PHP의 ReflectionClass는 클래스의 정보를 가져올 수 있는 클래스입니다. ReflectionClass::getInterfaceNames() 메소드는 클래스가 implements 한 인터페이스의 이름을 가져옵니다.
예제
#hostingforum.kr
php
// 인터페이스 정의
interface Animal {
public function sound();
}
interface Mammal {
public function eat();
}
// 클래스 정의
class Dog implements Animal, Mammal {
public function sound() {
echo "Woof!
";
}
public function eat() {
echo "Dog is eating.
";
}
}
// ReflectionClass 사용
$reflection = new ReflectionClass('Dog');
$interfaces = $reflection->getInterfaceNames();
// 인터페이스 이름 출력
echo "Dog 클래스는 다음 인터페이스를 implements합니다.
";
foreach ($interfaces as $interface) {
echo "- $interface
";
}
결과
#hostingforum.kr
Dog 클래스는 다음 인터페이스를 implements합니다.
- Animal
- Mammal
설명
* 인터페이스 `Animal`과 `Mammal`을 정의합니다.
* 클래스 `Dog`를 정의하고, `Animal`과 `Mammal` 인터페이스를 implements합니다.
* `ReflectionClass`를 사용하여 `Dog` 클래스의 정보를 가져옵니다.
* `getInterfaceNames()` 메소드를 사용하여 `Dog` 클래스가 implements 한 인터페이스의 이름을 가져옵니다.
* 가져온 인터페이스 이름을 출력합니다.
참고
* `ReflectionClass`는 클래스의 정보를 가져올 수 있는 클래스입니다.
* `getInterfaceNames()` 메소드는 클래스가 implements 한 인터페이스의 이름을 가져옵니다.
* 인터페이스는 클래스가 implements 한 메소드의 선언을 포함합니다.
* 인터페이스는 클래스의 구현을 강제하지 않습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.