PHP တွင် OOP အတွက် class များအတွင်း method များကို ခေါ်ဆိုခြင်း
တစ်ခုသော method များကို အခြားသော method များအတွင်း $this မှတစ်ဆင့် ခေါ်ဆိုနိုင်ပါသည်။
ဥပမာတစ်ခုဖြင့် ကြည့်ကြပါစို့။ ကျွန်ုပ်တို့တွင် user class တစ်ခုနှင့် property တစ်ခုကို ပြန်ပေးသော method တစ်ခုရှိသည်ဆိုပါစို့။
<?php
class User {
public $name;
public function show() {
return $this->name;
}
}
?>
ကျွန်ုပ်တို့တွင် စာကြောင်း၏ပထမစာလုံးကို အကြီးစာလုံးပြောင်းပေးသော cape method တစ်ခုလည်းရှိသည်ဆိုပါစို့။
<?php
class User {
public $name;
public function show() {
return $this->name;
}
public function cape($str) {
return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1);
}
}
?>
ယခု show method အတွင်း cape method ကို အသုံးပြုကြည့်ကြပါစို့။
<?php
class User {
public $name;
public function show() {
return $this->cape($this->name);
}
public function cape($str) {
return mb_strtoupper(mb_substr($str, 0, 1)) . mb_substr($str, 1);
}
}
?>
name နှင့် surn ဆိုသော properties များပါရှိသော Student class တစ်ခုကို ဖန်တီးပါ။
စာကြောင်း၏ပထမစာလုံးကိုရယူပြီး အကြီးစာလုံးပြောင်းပေးမည့် အကူ method တစ်ခုကို ဖန်တီးပါ။
ကျောင်းသား၏ အမည်ဦးနှင့် မိသားစုအမည်၏ ပထမစာလုံးများဖြစ်သော အမှတ်အသားများကို ပြန်ပေးမည့် method တစ်ခုကို ဖန်တီးပါ။