
ReflectionClass::isInterface 함수는 클래스의 타입을 확인하는 데 사용됩니다. 이 함수를 사용하여 인터페이스를 확인할 때, 클래스가 인터페이스를 상속하는지 여부를 확인할 수 있습니다.
인터페이스와 클래스의 차이점은 다음과 같습니다.
- 인터페이스는 추상 클래스로, 클래스가 구현해야 하는 메서드를 선언합니다. 인터페이스는 클래스가 구현해야 하는 메서드의 목록을 제공합니다.
- 클래스는 인터페이스를 상속하거나 구현할 수 있습니다. 클래스가 인터페이스를 상속하면, 클래스는 인터페이스의 메서드를 상속받습니다. 클래스가 인터페이스를 구현하면, 클래스는 인터페이스의 메서드를 구현해야 합니다.
예를 들어, 다음 코드를 살펴보겠습니다.
#hostingforum.kr
php
interface Animal {
public function sound();
}
class Dog implements Animal {
public function sound() {
echo "멍멍!";
}
}
class Cat implements Animal {
public function sound() {
echo "야옹!";
}
}
$dog = new Dog();
$cat = new Cat();
var_dump(class_implements('Dog')); // array(1) { [0]=> string(6) "Animal" }
var_dump(class_implements('Cat')); // array(1) { [0]=> string(6) "Animal" }
$reflection = new ReflectionClass('Dog');
echo $reflection->isInterface() ? 'true' : 'false'; // false
echo $reflection->isInstantiable() ? 'true' : 'false'; // true
$reflection = new ReflectionClass('Animal');
echo $reflection->isInterface() ? 'true' : 'false'; // true
echo $reflection->isInstantiable() ? 'true' : 'false'; // false
위 코드에서, `class_implements` 함수는 클래스가 인터페이스를 상속하는지 여부를 확인합니다. `ReflectionClass::isInterface` 함수는 클래스가 인터페이스인지 여부를 확인합니다. `ReflectionClass::isInstantiable` 함수는 클래스가 인스턴스화할 수 있는지 여부를 확인합니다.
따라서, ReflectionClass::isInterface 함수를 사용하여 인터페이스를 확인할 때, 클래스가 인터페이스를 상속하는지 여부를 확인할 수 있습니다.
2025-06-30 04:28