phpclass/클래스활용2000. 10. 10. 14:27
호환성
PHP4 >= 4.0b2
기능
객체나 클래스의 부모클래스명을 반환
형식
string get_parent_class (mixed obj)
설명
obj가 객체이면 인스턴스화할 때 사용된 클래스의 부모클래스명을 반환합니다. obj가 문자열이면, 그 이름을 갖는 클래스의 부모클래스명을 반환합니다. 이 기능은 PHP 4.0.5에서 추가되었습니다.
[code php;gutter:false] <?php

// base class with member properties and methods
class Vegetable {
var $edible;
var $color;

function Vegetable( $edible, $color="green" ) {
$this->edible = $edible;
$this->color = $color;
}

function is_edible() {
return $this->edible;
}

function what_color() {
return $this->color;
}
} // end of class Vegetable

// extends the base class
class Spinach extends Vegetable {
var $cooked = false;

function Spinach() {
$this->Vegetable( true, "green" );
}

function cook_it() {
$this->cooked = true;
}

function is_cooked() {
return $this->cooked;
}
} // end of class Spinach

$obj = new Spinach;

echo "객체 \$obj를 생성했던 클래스는 ".get_class($obj)."이며,
그 부모클래스는 ".get_parent_class($obj)."입니다.";

?> [/code]
< 예제 소스 >
객체 $obj를 생성했던 클래스는 spinach이며, 그 부모클래스는 vegetable입니다.
< 실행 결과 >
참고
get_class(), is_subclass_of()

Posted by 방글24