PHP OOP ရှိ အမွေဆက်ခံခြင်းအတွင်း Private Method များ

Private method များကို အမွေဆက်ခံ၍ မရပါ။ ၄င်းကို encapsulation ကို မချိုးဖောက်စေရန် အထူးပြုလုပ်ထားသည်။ ဥပမာတစ်ခုအား ကြည့်ရှုကြပါစို့။ ကျွန်ုပ်တို့တွင် private method ပါရှိသော မိဘ class အောက်ပါအတိုင်းရှိသည်ဆိုပါစို့။

php<?php
  class User {
    private $name;
    
    public function setName($name) {
      $this->name = $name;
    }
    public function getName() {
      return $this->capeFirst($this->name);
    }
    
    private function capeFirst($str) {
      return ucfirst($str);
    }
  }
?>

မိဘ class မှ အောက်ပါ class ကို အမွေဆက်ခံသည်ဆိုပါစို့။

php<?php
  class Student extends User {
    private $surn;
    
    public function setSurn($surn) {
      $this->surn = $surn;
    }
    public function getSurn() {
      return $this->surn;
    }
  }
?>

သားသမီး class သည် မိဘ၏ private method ကို အသုံးပြုလိုပါက၊ PHP သည် ခွင့်မပြုဘဲ error တစ်ခုကို ပြန်ပေးပါလိမ့်မည်။

php<?php
  class Student extends User {
    private $surn;
    
    public function setSurn($surn) {
      $this->surn = $surn;
    }
    public function getSurn() {
      return $this->capeFirst($this->surn); // error ဖြစ်ပါမည်
    }
  }
?>
Задание 1

Employee class အတွင်း မိဘ၏ private method ကို အသုံးပြုရန် ကြိုးစားကြည့်ပါ။