PHP OOP - အမွေဆက်ခံခြင်း။
PHP - Inheritance ဆိုတာဘာလဲ။
OOP inheritance = အတန်းတစ်ခုသည် အခြားအတန်းမှဆင်းသက်လာသောအခါ။
ကလေးအတန်းအစားသည် မိဘအတန်းအစားထံမှ အများသူငှာ ကာကွယ်ထားသော ဂုဏ်သတ္တိများနှင့် နည်းလမ်းအားလုံးကို အမွေဆက်ခံပါမည်။ ထို့အပြင်၎င်းသည်၎င်း၏ကိုယ်ပိုင်ဂုဏ်သတ္တိများနှင့်နည်းလမ်းများရှိသည်။
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 class သည် Fruit class မှ ဆင်းသက်လာသည်။
ဆိုလိုသည်မှာ Strawberry class သည် public $name နှင့် $color properties အပြင် public __construct() နှင့် intro() method ကို အမွေဆက်ခံရသောကြောင့် Fruit class မှ သုံးနိုင်သည်။
စတော်ဘယ်ရီအတန်းတွင်လည်း ၎င်း၏ကိုယ်ပိုင်နည်းလမ်း ရှိသည်- message()။
PHP - Inheritance နှင့် Protected Access Modifier
ယခင်အခန်း 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
?>
အထက်ဖော်ပြပါ ဥပမာ protected
တွင် အတန်းပြင်ပမှ method (intro()) ကို ခေါ်ရန် ကြိုးစားပါက error ရရှိမည်ကို ကျွန်ုပ်တို့တွေ့မြင်ရပါသည်။ 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
အဘယ်ကြောင့်ဆိုသော် ကျွန်ုပ်တို့သည် ဆင်းသက်လာသော class အတွင်းမှ method (intro()) ကို ခေါ်သောကြောင့်
ဖြစ်သည်။
PHP - အမွေဆက်ခံသည့်နည်းလမ်းများကို ဦးစားပေးခြင်း။
ကလေးအတန်းအစားရှိ နည်းလမ်းများ (အမည်တူ) ကို ပြန်လည်သတ်မှတ်ခြင်းဖြင့် အမွေဆက်ခံသည့်နည်းလမ်းများကို လွှမ်းမိုးနိုင်သည်။
အောက်ပါဥပမာကိုကြည့်ပါ။ ကလေးအတန်းအစား (Strawberry) ရှိ __construct() နှင့် intro() နည်းလမ်းများသည် ပင်မအတန်းရှိ __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
}
}
?>