第 13 章 クラスとオブジェクト

目次
クラス
extends
コンストラクタ
::
親クラス
Serializing objects - objects in sessions
The magic functions __sleep and __wakeup
コンストラクタの内部での参照

クラス

クラスは、変数およびこれらの変数で動作する関数の集まりです。 クラスは次のような構文により定義されます。


<?php
class Cart {
    var $items;  // 買い物篭の中のアイテム
   
    // $num 個の $artnr を買い物篭に加えます
 
    function add_item ($artnr, $num) {
        $this->items[$artnr] += $num;
    }
   
    // $num 個の $artnr を買い物籠から出します
 
    function remove_item ($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } else {
            return false;
        }   
    }
}
?>
     

この例は、籠の中の物と籠にアイテムを加えたり除いたりする二つの関数への 連想配列からなる Cart という名前のクラスを定義します。

注意

以下の注意書きはPHP 4に関するものです。

名前stdClassは、Zendにより内部的に使用され、保 存されています。PHPでstdClassという名前のクラ スを使用することはできません。

The function names __sleep and __wakeup are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them. See below for more information.

PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

注意 PHP 4では、変数varについては定数による初期化の みが可能です。定数以外で初期化を行う場合には初期化関数が必要です。 この初期化関数は、オブジェクトがクラスから構築される際に自動的に コールされます。このような関数はコンストラクタと呼ばれます。(以下 参照)


/* 以下のコードはPHP 4では動作しません。 */
class Cart {
    var $todays_date = date("Y-m-d");
    var $name = $firstname;
    var $owner = 'Fred ' . 'Jones';
}

/* 以下に正しい方法を示します。 */
class Cart {
    var $todays_date;
    var $name;
    var $owner;

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

クラスは型、つまり、実際の変数の雛型です。new演 算子により所望の型の変数を作成する必要があります。


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

  $another_cart = new Cart;
  $another_cart->add_item("0815", 3);
    

この例は、クラスCartのオブジェクト $cartおよび$another_cartを作成 します。$cartオブジェクトの関数 add_item()が商品番号10の商品一つが カートに追加されています。商品番号0815の商品3つが$another_cartに追 加されています。

Both, $cart and $another_cart, have functions add_item(), remove_item() and a variable items. These are distinct functions and variables. You can think of the objects as something similar to directories in a filesystem. In a filesystem you can have two different files README.TXT, as long as they are in different directories. Just like with directories where you'll have to type the full pathname in order to reach each file from the toplevel directory, you have to specify the complete name of the function you want to call: In PHP terms, the toplevel directory would be the global namespace, and the pathname separator would be ->. Thus, the names $cart->items and $another_cart->items name two different variables. Note that the variable is named $cart->items, not $cart->$items, that is, a variable name in PHP has only a single dollar sign.


// correct, single $
$cart->items  = array("10" => 1); 

// invalid, because $cart->$items becomes $cart->""
$cart->$items = array("10" => 1);

// correct, but may or may not be what was intended:
// $cart->$myvar becomes $ncart->items
$myvar = 'items';
$cart->$myvar = array("10" => 1);  
    

Within a class definition, you do not know under which name the object will be accessible in your program: At the time the Cart class was written, it was unknown that the object will be named $cart or $another_cart later. Thus, you cannot write $cart->items within the Cart class itself. Instead, in order to be able to access it's own functions and variables from within a class, one can use the pseudo-variable $this which can be read as 'my own' or 'current object'. Thus, '$this->items[$artnr] += $num' can be read as 'add $num to the $artnr counter of my own items array' or 'add $num to the $artnr counter of the items array within the current object'.