IX. 클래스/객체 함수

소 개

About

여기에 있는 함수들은 클래스와 객체에 대한 정보를 얻는데 도움을 줍니다. 여러분은 이 함수들을 사용하여 어떤 객체가 속한 클래스의 이름, 그리고 그 객체의 속성(멤버 함수(메소드,method)와 속성(멤버변수 등.))을 찾아낼수 있습니다. 여기있는 함수들을 사용하면 어떤 객체의 속성및 메소드뿐만 아니라, 그 객체의 혈통도 알아낼 수 있습니다.(즉, 그 객체가 어떤 클래스의 상속을 받았는지를 알아낼 수 있습니다.)

사용방법

여기에 있는 예제에서는 먼저 기본적인 형태의 클래스를 정의하고, 그 클래스의 상속을 받은 클래스를 정의합니다. 여기의 기본적인 형태의 클래스는 일반적인 야채에 대한 클래스로, 그 야채가 먹을수 있는 것인지 혹은 그렇지 않은지, 또한 그 야채의 색깔은 무엇인지를 정의합니다. 그 클래스의 하위클래스(자식 클래스)는 시금치 클래스로 요리를 할수 있는 멤버함수(메소드, method)와 "시금치 객체가 요리를 할수 있는지를 알아내는 다른 멤버함수들을 추가 합니다.

예 1. classes.inc

<?php

// 멤버변수(속성)와 멤버함수를 가지는 기본 클래스
	
    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

// 기본클랙스의 상속(확장)
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;
    }
    
} // 시금치 객체의 끝

?>

위의 클래스들로 부터 2개의 객체를 생성하면, 그 클래스의 혈통을 포함한 클래스들에 대한 정보를 알아낼수 있습니다. 변수들을 깔끔하게 출력하기 위해 몇개의 멤버함수들을 추가해서 정의할수 있습니다.

예 2. test_script.php

<pre>
<?php

include "classes.inc";

// utility functions

function print_vars($obj) {
    $arr = get_object_vars($obj);
    while (list($prop, $val) = each($arr))
        echo "\t$prop = $val\n";
}

function print_methods($obj) {
    $arr = get_class_methods(get_class($obj));
    foreach ($arr as $method)
        echo "\tfunction $method()\n";
}

function class_parentage($obj, $class) {
    global $$obj;
    if (is_subclass_of($$obj, $class)) {
        echo "Object $obj belongs to class ".get_class($$obj);
        echo " a subclass of $class\n";
    } else {
        echo "Object $obj does not belong to a subclass of $class\n";
    }
}

// instantiate 2 objects

$veggie = new Vegetable(true,"blue");
$leafy = new Spinach();

// print out information about objects
echo "veggie: CLASS ".get_class($veggie)."\n";
echo "leafy: CLASS ".get_class($leafy);
echo ", PARENT ".get_parent_class($leafy)."\n";

// show veggie properties
echo "\nveggie: Properties\n";
print_vars($veggie);

// and leafy methods
echo "\nleafy: Methods\n";
print_methods($leafy);

echo "\nParentage:\n";
class_parentage("leafy", "Spinach");
class_parentage("leafy", "Vegetable");
?>
</pre>

위의 예제에서 중요한 것은, $leafy 객체는 Vegetable 클래스의 하위클래스인 시금치클래스의 객체라는 것입니다. 위의 예제는 다음과 같은 결과가 출력됩니다.

[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable

차례
call_user_method_array --  Call a user method given with an array of parameters [deprecated]
call_user_method --  주어진 객체의 사용자 메소드(멤버함수)를 호출합니다.
class_exists -- 클래스가 정의 되어있는지 검사합니다.
get_class_methods -- 클래스의 기본적인 속성(멤버변수)들을 배열로 반환합니다.
get_class_vars --  클래스의 기본적인 속성(멤버변수)들을 배열로 반환합니다.
get_class -- 어떤(임의의) 객체가 속한 클래스의 이름을 반환합니다.
get_declared_classes -- Returns an array with the name of the defined classes
get_object_vars -- 객체의 속성(멤버변수)들을 associative 배열로 반환한다.
get_parent_class -- 객체의 상위(아버지) 클래스의 이름을 반환합니다.
is_a --  Returns TRUE if the object is of this class or has this class as one of its parents
is_subclass_of --  인수로 주어진 클래스의 하위(자식)클래스의 객체인지 검사합니다.
method_exists -- 클래스의 멤버함수(메소드)가 존재하는지 검사합니다.