호환성
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]
// 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()
'phpclass > 클래스활용' 카테고리의 다른 글
{객체에 관련된 정보}1.클래스와 인스턴스 (0) | 2000.10.17 |
---|---|
{클래스&객체함수}14.method_exists (0) | 2000.10.10 |
{클래스&객체함수}12.is_a (0) | 2000.10.10 |
{클래스&객체함수}11.get_parent_class (0) | 2000.10.10 |
{클래스&객체함수}10.get_object_vars (0) | 2000.10.10 |