PHP OOP တွင် Trait method များ၏ ဝင်ရောက်ခွင့် ပြောင်းလဲခြင်း

Trait အတွင်း၌ method များအတွက် မည်သည့်ဝင်ရောက်ခွင့် ပြင်ဆင်ချက်ကိုမဆို အသုံးပြုနိုင်ပါသည် (ဆိုလိုသည်မှာ public၊ private သို့မဟုတ် protected ဖြစ်ပါသည်)။ သို့သော် လိုအပ်ပါက class အတွင်းမှ ၎င်းပြင်ဆင်ချက်ကို အခြားတစ်ခုသို့ ပြောင်းလဲနိုင်ပါသည်။ ဤသို့ပြုလုပ်ရန် use body အတွင်း၊ သော့ချက်စကားလုံး as ပြီးနောက် ဝင်ရောက်ခွင့်ပြင်ဆင်ချက် အသစ်ကို သတ်မှတ်ပေးရပါမည်။

ဥပမာတစ်ခုဖြင့် ကြည့်ကြပါစို့။ ကျွန်ုပ်တို့တွင် private method ပါသည့် အောက်ပါ Trait ရှိသည်ဆိုပါစို့။

php<?php
  trait TestTrait
  {
    private function method()
    {
      return '!!!';
    }
  }
?>

ကျွန်ုပ်တို့၏ Trait ကို class သို့ တွဲထည့်ကြပါစို့။

php<?php
  class Test
  {
    use TestTrait;
  }
?>

Class အတွင်းရှိ method ကို public အဖြစ်သို့ ပြောင်းလဲကြပါစို့။

php<?php
  class Test
  {
    use TestTrait {
      TestTrait::method as public;
    }
  }
?>

Class အပြင်ဘက်မှ public method ၏ အလုပ်လုပ်ပုံကို စစ်ဆေးကြည့်ကြပါစို့။

php<?php
  $test = new Test;
  echo $test->method(); // '!!!' ကို ထုတ်ပြမည်
?>