Forum und email

類別和物件

Table of Contents

類別

類別的概念簡單來說就是一小段(也可能是一大段)有一個名稱的程式段, 其內有變數以及使用這些變數的函數。 定義類別的句法請看下列示範:

<?php
class Cart {
    var 
$items;  // Items in our shopping cart
   
    // Add $num articles of $artnr to the cart
 
    
function add_item ($artnr$num) {
        
$this->items[$artnr] += $num;
    }
   
    
// Take $num articles of $artnr out of the cart
 
    
function remove_item ($artnr$num) {
        if (
$this->items[$artnr] > $num) {
            
$this->items[$artnr] -= $num;
            return 
true;
        } else {
            return 
false;
        }   
    }
}
?>

上面定義了一個叫做 Cart 的類別(按: 模擬網上購物車)。 它包括了一個名稱索引式的陣列以兩個分別用來在陣列中加入及扣除物品的函數。

Note: In PHP 4, only constant initializers for var variables are allowed. Use constructors for non-constant initializers.

/* None of these will work in PHP 4. */
class Cart {
    var $todays_date = date("Y-m-d");
    var $name = $firstname;
    var $owner = 'Fred ' . 'Jones';
}

/* This is how it should be done. */
class Cart {
    var $todays_date;
    var $name;
    var $owner;

    function Cart() {
        $this->todays_date = date("Y-m-d");
        $this->name = $GLOBALS['firstname'];
        /* etc. . . */
    }
}

類別是一種變數的形態。 也就是說像 integer、 string 等何以把某類別形態指派給變數。 不同的是建立某個類別的變數需要用到 new 指令 (按: 這樣產生的變數術語上叫做建立了一個物件):

$cart = new Cart;
$cart->add_item("10", 1);

上例建立了一個叫做 cart 的物件, 它的類別是 $Cart。 (還記得 PHP 中大小寫是不一樣的嗎 ?) 跟著調用該物件中的 add_item 函數在陣列中加入一個編號 '10' 的東西。

類別也可以是其他類別的延伸。 新的類別繼承了舊的類別之外還可以加上自己的變數及功能。 定義延伸類別的方法是定義類別時加上 extend 這個關鍵字。 不過不準定多重繼承關係。

class Named_Cart extends Cart {
    var $owner;
  
    function set_owner ($name) {
        $this->owner = $name;
    }
}

上面新定義了 Named_Cart 類別, 它擁有 Cart 類別中所有的變數及函數另加上一個存用戶名字的變數及存取該變數的函數: set_owner()。 你可用老方法根據新類別建立一個物件, 現在這個物件除了可以像以前一樣加減東西外還可以設定用戶名字:

$ncart = new Named_Cart;    // Create a named cart
$ncart->set_owner ("kris"); // Name that cart
print $ncart->owner;        // print the cart owners name
$ncart->add_item ("10", 1); // (inherited functionality from cart)

在定義類別時 $this 這個變數是指物件本身。 在物件之中你一定要用 $this->something 的句式來調用或存取物件中的變數和函數。 。 Both in and outside of the object you do not need a $ when accessing an object's properties.

$ncart->owner  = "chris"; // no $

$ncart->$owner = "chris";
// this is invalid because $ncart->$owner = $ncart->""

$myvar = 'owner';
$ncart->$myvar = "chris";  
// this is valid because $ncart->$myvar = $ncart->owner

Constructors 建造者 是類別中的一個函數。 它會在每個根據該類別產生物件時自動被執行。 把函數變成建造者函數的方法是把該函數命名成和類別的名字一樣。

class Auto_Cart extends Cart {
    function Auto_Cart () {
        $this->add_item ("10", 1);
    }
}

上面我們定義了 Auto_Cart 類別。 它是一個 Cart 類別加上了一個建造者函數, 這函數會自動會在陣列 (或者說是購物單) 中加上一個 '10' 號的東西。 另外建造者函數也可以接受參數, 這令建立物件時有更大的彈性。

class Constructor_Cart extends Cart {
    function Constructor_Cart ($item = "10", $num = 1) {
        $this->add_item ($item, $num);
    }
}
 
// Shop the same old boring stuff.
 
$default_cart   = new Constructor_Cart;
 
// Shop for real...
 
$different_cart = new Constructor_Cart ("20", 17);
Caution

對於繼承類別而言, 如果子類別和父類別都有建造者函數話, 建立繼承類別的物件時並不會同時調用母類別的建造者函數。