PHP ရှိ OOP တွင် Static Methods များ

Classes များနှင့် အလုပ်လုပ်စဉ်တွင်၊ object တစ်ခုဖန်တီးရန်မလိုဘဲ ၎င်းတို့ကိုခေါ်ဆိုရန် လုပ်ဆောင်နိုင်သော methods များကို ပြုလုပ်နိုင်ပါသည်။ �ိုကဲ့သို့သော methods များကို static methods ဟုခေါ်သည်။ Method တစ်ခုကို static ဖြစ်ကြောင်း ကြေငြာလိုပါက၊ access modifier ပြီးနောက်တွင် သော့ချက်စကားလုံး static ကို ရေးရပါမည်။

Test.php<?php
  class Test
  {
    public static function method()
    {
      return '!!!';
    }
  }
?>

Static method တစ်ခုကို ရည်ညွှန်းရန်၊ class ၏အမည်ကိုရေး၊ ထို့နောက် colon နှစ်ခု ပြီးနောက် method ၏အမည်ကို ရေးရပါမည်။ ထိုသို့လုပ်ဆောင်ရာတွင် class ၏ object ကို ဖန်တီးရန်မလိုပါ။ ဤသို့ဖြစ်သည်။

index.php<?php
  echo Test::method(); // '!!!' ကိုထုတ်ပြမည်
?>
Задание 1

အောက်ပါ class ၏ methods များကို static ဖြစ်အောင် ပြောင်းလဲပါ။

php<?php
  class Math
  {
    public function getSum($a, $b)
    {
      return $a + $b;
    }
    
    public function getProduct($a, $b)
    {
      return $a * $b;
    }
  }
?>