PHPOOP-継承
PHP-継承とは何ですか?
OOPでの継承=クラスが別のクラスから派生する場合。
子クラスは、親クラスからすべてのパブリックおよび保護されたプロパティとメソッドを継承します。さらに、独自のプロパティとメソッドを持つことができます。
継承されたクラスは、extends
キーワードを使用して定義されます。
例を見てみましょう:
例
<?php
class Fruit {
public
$name;
public $color;
public
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class
Strawberry extends Fruit {
public
function message() {
echo "Am I a fruit or a
berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
例の説明
Strawberryクラスは、Fruitクラスから継承されます。
つまり、Strawberryクラスは、継承により、Fruitクラスのpublic $ nameプロパティと$ colorプロパティ、およびpublic __construct()メソッドとintro()メソッドを使用できます。
Strawberryクラスには、独自のメソッドmessage()もあります。
PHP-継承と保護されたアクセス修飾子
前の章では、protected
プロパティまたはメソッドにクラス内で、およびそのクラスから派生したクラスからアクセスできることを学びました。どういう意味ですか?
例を見てみましょう:
例
<?php
class Fruit {
public
$name;
public $color;
public
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
class
Strawberry extends Fruit {
public
function message() {
echo "Am I a fruit or a
berry? ";
}
}
// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red");
// OK. __construct() is public
$strawberry->message(); // OK. message()
is public
$strawberry->intro(); // ERROR. intro()
is protected
?>
上記の例では、クラスの外部からメソッド(intro())を呼び出そうとするとprotected
、エラーが発生することがわかります。public
メソッドは正常に機能します!
別の例を見てみましょう:
例
<?php
class Fruit {
public $name;
public
$color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The
fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
// Call protected
method from within derived class - OK
$this ->
intro();
}
}
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is
public
$strawberry->message(); // OK. message() is
public and it calls intro() (which is protected) from within the
derived class
?>
上記の例では、すべてが正常に機能していることがわかります。これは
protected
、派生クラス内からメソッド(intro())を呼び出すためです。
PHP-継承されたメソッドのオーバーライド
継承されたメソッドは、子クラスのメソッドを再定義する(同じ名前を使用する)ことでオーバーライドできます。
以下の例を見てください。子クラス(Strawberry)の__construct()メソッドとintro()メソッドは、親クラス(Fruit)の__construct()メソッドとintro()メソッドをオーバーライドします。
例
<?php
class Fruit {
public
$name;
public $color;
public
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
class
Strawberry extends Fruit {
public $weight;
public
function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "The fruit is {$this->name}, the color is {$this->color},
and the weight is {$this->weight} gram.";
}
}
$strawberry = new Strawberry("Strawberry", "red",
50);
$strawberry->intro();
?>
PHP-最後のキーワード
このfinal
キーワードを使用して、クラスの継承を防止したり、メソッドのオーバーライドを防止したりできます。
次の例は、クラスの継承を防ぐ方法を示しています。
例
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
次の例は、メソッドのオーバーライドを防ぐ方法を示しています。
例
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>