XII. Classes/Objects 類/物件函數

簡介

本類函數容許取得類和物件案例的訊息。可以取得物件所屬的類的名字,以及它的成員屬性和方法。通過使用這些函數,不僅可以弄清楚一個物件類的全體成員,而且可以知道它的起源(例如,該物件類是哪個類的增加)。

需求

要編譯本增加模組無需外部庫檔案。

安裝

本增加模組作為 PHP 內核的一部分,無需安裝即可使用。

運行時配置

本增加模組在 php.ini 中未定義任何配置選項。

資源類型

本增加模組未定義任何資源類型。

預定義常量

本增加模組未定義任何常量。

範例

在本例子中,先定義一個基礎類和一個增加類。基礎類描述的是普通的蔬菜(Vegetable),它是否可食用(is_edible)以及它是什麼彩色(what_color)。子類 Spinach 增加了「烹調」的方法(cook_it)和「獲知它是否已被烹調了」的方法(is_cooked)。

例子 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;
    }

// 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;
    }

// Spinach 類定義結束

?>

接著,從這些物件中建立兩個物件案例,列印出它們的關聯訊息,內含它們的起源。還定義了一些實用函數,它們主要是為了讓變量的輸出好看些。

例子 2. test_script.php

<pre>
<?php

include "classes.inc";

// utility functions

function print_vars($obj)
{
    foreach (
get_object_vars($obj) as $prop => $val) {
        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)
{
    if (
is_subclass_of($GLOBALS[$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>

上邊例子中重點要注意的是物件 $leafySpinach 類的一個案例,而 Spinach 類是 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_user_method --  呼叫特定對象的會員方法〔已停用〕
class_exists -- 檢查類是否已定義
get_class_methods -- 返回由類的方法名組成的陣列
get_class_vars --  返回由類的預設屬性組成的陣列
get_class -- 返回對象的類名
get_declared_classes -- 返回由已定義類的名字所組成的陣列
get_declared_interfaces --  Returns an array of all declared interfaces
get_object_vars -- 返回由物件屬性組成的關聯陣列
get_parent_class -- 返回物件或類的父類名
interface_exists -- Checks if the interface has been defined
is_a --  若果物件屬於該類或該類是此對象的父類則返回 TRUE
is_subclass_of --  若果此對象是該類的子類,則返回 TRUE
method_exists -- 檢查類的方法是否存在
property_exists --  Checks if the object or class has a property