phpclass/클래스활용2000. 10. 10. 14:29
호환성
PHP4 >= 4.0b4
기능
객체가 지정된 클래스의 서브클래스에 속하는지 확인
형식
bool is_subclass_of (object obj, string superclass)
설명
객체 obj가 superclass의 서브클래스라면 true를 반환하고, 그렇게 않으면 false를 반환합니다.
[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;

if (is_subclass_of($obj, "vegetable")) {
echo "객체 \$obj는 ".get_parent_class($obj)
."의 서브클래스로부터 생성된 객체입니다.";
} else {
echo "객체 \$obj는 ".get_parent_class($obj)
."의 서브클래스로부터 생성된 객체가 아닙니다.";
}

?> [/code]
< 예제 소스 >
객체 $obj는 vegetable의 서브클래스로부터 생성된 객체입니다.
< 실행 결과 >
참고
get_class(), get_parent_class()

Posted by 방글24