PHP OOP တွင် Static Method များနှင့် $this အသုံးပြုခြင်း
Static method များအတွင်း၌ $this ကို အသုံးပြု၍မရပါ။
ဤသို့ဖြစ်ရခြင်းမှာ static method များသည်
object context အပြင်မှပင် class နာမည်ကို တိုက်ရိုက်ခေါ်ယူပြီး
အလုပ်လုပ်နိုင်ခြင်းကြောင့်ဖြစ်သည်။
ပုံမှန် method များအတွင်း၌ static နှင့် ပုံမှန် properties နှင့် methods များကို ခေါ်ယူအသုံးပြုနိုင်သည်။
ဥပမာများဖြင့် ကြည့်ရှုကြပါစို့။ ပုံမှန် method အတွက် ဥပမာ -
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public function method()
{
echo self::$staticProperty; // 'static' ကို ပြသမည်
echo $this->usualProperty; // 'usual' ကို ပြသမည်
}
}
$test = new Test;
$test->method();
?>
Static method အတွက် ဥပမာ -
<?php
class Test
{
public static $staticProperty = 'static';
public $usualProperty = 'usual';
public static function method()
{
echo self::$staticProperty; // 'static' ကို ပြသမည်
echo $this->usualProperty; // error ပြနေမည်
}
}
$test = new Test;
$test::method();
?>