コンストラクタ

注意

In PHP 3 and PHP 4 constructors behave differently. The PHP 4 semantics are strongly preferred.

コンストラクタは、newによりクラスの新しいインス タンスを作成する際に自動的にコールされるクラス関数です。 PHP 3では、ある関数が、クラス名と同じ名前を有している場合にコンス トラクタになりました。PHP 4では、ある関数が、そのコンストラクタが 定義されているクラスと同じ名前を有している場合にコンストラクタにな ります。これらの違いは僅かですが、重要な違いがあります。(以下、参 照)


// Works in PHP 3 and PHP 4.
class Auto_Cart extends Cart {
    function Auto_Cart () {
        $this->add_item ("10", 1);
    }
}
    

この例は、Cart にコンストラクタを加えたクラス Auto_Cart を定義しています。 このコンストラクタは、"new" により新しい Auto_Cart が作成される度に 籠に10番の物を一つ保持するように初期化します。コンストラクタは、オ プションとして引数をとります。これにより、コンストラクタは非常に便 利なものとなります。To be able to still use the class without parameters, all parameters to constructors should be made optional by providing default values.


// Works in PHP 3 and PHP 4.
class Constructor_Cart extends Cart {
    function Constructor_Cart ($item = "10", $num = 1) {
        $this->add_item ($item, $num);
    }
}
 
// しつこいが、前の例と同じものを買う
 
$default_cart   = new Constructor_Cart;
 
// 実際に買うもの篭に入れる...
 
$different_cart = new Constructor_Cart ("20", 17);
    

注意

In PHP 3, derived classes and constructors have a number of limitations. The following examples should be read carefully to understand these limitations.


class A {
  function A() {
    echo "I am the constructor of A.<br>\n";
  }
}

class B extends A {
  function C() {
    "I am a regular function.<br>\n";
  }
}

// no constructor is being called in PHP 3.
$b = new B;
    

In PHP 3, no constructor is being called in the above example. The rule in PHP 3 is: 'A constructor is a function of the same name as the class.'. The name of the class is B, and there is no function called B() in class B. Nothing happens.

This is fixed in PHP 4 by introducing another rule: If a class has no constructor, the constructor of the base class is being called, if it exists. The above example would have printed 'I am the constructor of A.<br>' in PHP 4.


class A {
  function A() {
    echo "I am the constructor of A.<br>\n";
  }
  
  function B() {
    echo "I am a regular function named B in class A.<br>\n";
    echo "I am not a constructor in A.<br>\n";
  }
}

class B extends A {
  function C() {
    echo "I am a regular function.<br>\n";
  }
}

// This will call B() as a constructor.
$b = new B;
    

In PHP 3, the function B() in class A will suddenly become a constructor in class B, although it was never intended to be. The rule in PHP 3 is: 'A constructor is a function of the same name as the class.'. PHP 3 does not care if the function is being defined in class B, or if it has been inherited.

This is fixed in PHP 4 by modifying the rule to: 'A constructor is a function of the same name as the class it is being defined in.'. Thus in PHP 4, the class B would have no constructor function of its own and the constructor of the base class would have been called, printing 'I am the constructor of A.<br>'.

注意

Neither PHP 3 nor PHP 4 call constructors of the base class automatically from a constructor of a derived class. It is your responsibility to propagate the call to constructors upstream where appropriate.

注意 There are no destructors in PHP 3 or PHP 4. You may use register_shutdown_function() instead to simulate most effects of destructors.

Destructors are functions that are called automatically when a variable is destroyed, either with unset() or by simply going out of scope. There are no destructors in PHP.