PHP OOP - Destructor
PHP - __destruct Function
အရာဝတ္တုကို ဖျက်ဆီးခြင်း သို့မဟုတ် ဇာတ်ညွှန်းကို ရပ်တန့်ခြင်း သို့မဟုတ် ထွက်သွားသည့်အခါ destructor ဟုခေါ်သည်။
အကယ်၍ သင်သည် __destruct()
လုပ်ဆောင်ချက်တစ်ခုကို ဖန်တီးပါက၊ PHP သည် script အဆုံးတွင် ဤလုပ်ဆောင်ချက်ကို အလိုအလျောက်ခေါ်ဆိုမည်ဖြစ်သည်။
destruct function သည် underscore နှစ်ခု (__) ဖြင့် စတင်သည်ကို သတိပြုပါ။
အောက်ဖော်ပြပါ ဥပမာတွင် class တစ်ခုမှ အရာဝတ္ထုတစ်ခုကို ဖန်တီးသည့်အခါ အလိုအလျောက်ခေါ်သည့် __construct() လုပ်ဆောင်ချက်နှင့် script ၏အဆုံးတွင် အလိုအလျောက်ခေါ်သည့် __destruct() လုပ်ဆောင်ချက် ပါရှိသည်-
ဥပမာ
<?php
class Fruit {
public
$name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
အခြားဥပမာ-
ဥပမာ
<?php
class Fruit {
public
$name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function __destruct() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
$apple = new Fruit("Apple", "red");
?>
အကြံပြုချက်- တည်ဆောက်သူများနှင့် destructors များက code ပမာဏကို လျှော့ချပေးသည့်အတွက် ၎င်းတို့သည် အလွန်အသုံးဝင်ပါသည်။