
get_parent_class() 함수는 PHP에서 클래스의 부모 클래스를 반환하는 내장 함수입니다.
예를 들어, 다음과 같은 클래스가 있습니다.
#hostingforum.kr
php
class Animal {
function sound() {
echo "동물은 소리를 낸다.";
}
}
class Dog extends Animal {
function sound() {
echo "개를 불린다.";
}
}
이 경우, Dog 클래스의 부모 클래스를 얻으려면 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$parent_class = get_parent_class('Dog');
echo $parent_class; // Animal
get_parent_class() 함수는 클래스의 이름을 인수로 전달할 수 있습니다.
또한, 이 함수는 객체의 부모 클래스를 얻을 수도 있습니다.
#hostingforum.kr
php
$dog = new Dog();
$parent_class = get_parent_class($dog);
echo $parent_class; // Animal
get_parent_class() 함수는 클래스의 상속 관계를 확인할 때 유용하게 사용할 수 있습니다. 예를 들어, 클래스의 메서드를 재정의할 때 부모 클래스의 메서드를 호출해야 하는 경우에 사용할 수 있습니다.
#hostingforum.kr
php
class Animal {
function sound() {
echo "동물은 소리를 낸다.";
}
}
class Dog extends Animal {
function sound() {
parent::sound(); // 부모 클래스의 메서드를 호출
echo "개를 불린다.";
}
}
get_parent_class() 함수는 PHP의 내장 함수이므로, 별도의 라이브러리를 설치할 필요가 없습니다.
2025-04-30 13:24