라이브러리
[PHP] ReflectionClass::getInterfaces - 인터페이스를 가져옵니다.
ReflectionClass::getInterfaces
PHP의 ReflectionClass::getInterfaces 메소드는 클래스의 인터페이스 목록을 반환합니다. 이 메소드는 ReflectionClass 인스턴스를 통해 호출할 수 있으며, 인터페이스 목록을 배열로 반환합니다.
예제
#hostingforum.kr
php
class Animal {
public function sound() {
echo "동물은 소리를 낸다.
";
}
}
interface Mammal {
public function eat();
}
interface Bird {
public function fly();
}
class Dog extends Animal implements Mammal, Bird {
public function eat() {
echo "강아지는 먹는다.
";
}
public function fly() {
echo "강아지는 날 수 없다.
";
}
}
$dog = new Dog();
// ReflectionClass 인스턴스 생성
$reflection = new ReflectionClass('Dog');
// 인터페이스 목록 가져오기
$interfaces = $reflection->getInterfaces();
// 인터페이스 목록 출력
echo "Dog 클래스는 다음 인터페이스를 구현한다:
";
foreach ($interfaces as $interface) {
echo "- " . $interface->getName() . "
";
}
결과
#hostingforum.kr
Dog 클래스는 다음 인터페이스를 구현한다:
- Mammal
- Bird
설명
위 예제에서, `Dog` 클래스는 `Mammal`과 `Bird` 인터페이스를 구현하고 있습니다. `ReflectionClass::getInterfaces` 메소드를 사용하여 `Dog` 클래스의 인터페이스 목록을 가져올 수 있습니다. 결과적으로, `Mammal`과 `Bird` 인터페이스가 출력됩니다.
참고
* `ReflectionClass` 클래스는 PHP 5.0.0부터 사용할 수 있습니다.
* `getInterfaces` 메소드는 인터페이스 목록을 반환합니다. 인터페이스 이름은 `getName` 메소드를 사용하여 가져올 수 있습니다.
* 인터페이스는 클래스가 구현할 수 있는 추상 메소드 집합입니다. 인터페이스는 클래스의 상속 관계에 영향을 미치지 않습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.