継承
継承により親クラスのメソッドやプロパティを引き継ぐ。
<?php require_once('menu.php'); class Drink extends Menu { // 独自プロパティ private $type; // オーバーライド public function __construct($name, $price, $image, $type) { parent::__construct($name, $price, $image); $this->type = $type; } // 独自メソッド public function getType() { return $this->type; } } ?>
require_once
他のファイルを読み込む。継承では継承元のファイルを読み込む必要がある。
extends
class 子クラス extends 親クラスで継承。
オーバーライド
親と同名のメソッドを定義してメソッドの中身を上書きできる。
<?php public function __construct($name, $price, $image, $type) { parent::__construct($name, $price, $image); $this->type = $type; } ?>
parent::メソッドを記述した箇所で親クラスのメソッドが呼び出される。
次の行で子クラスの独自プロパティに対する処理を加えている(オーバーライド)。