OOP မှာ PHP မှာ variable ထဲက method နာမည်

Property တွေရဲ့နာမည်တွေကို variable ထဲမှာ သိမ်းထားသလိုမျိုးပဲ method တွေရဲ့နာမည်တွေကိုလည်း သိမ်းထားနိုင်ပါတယ်။ ဥပမာတစ်ခုနဲ့ ကြည့်ရအောင်။ ကျွန်တော်တို့မှာ getter property တွေပါတဲ့ ဒီလို User class တစ်ခုရှိတယ်ဆိုပါစို့။

php<?php
  class User
  {
    private $name;
    private $surn;
    
    public function __construct($name, $surn)
    {
      $this->name = $name;
      $this->surn = $surn;
    }
    
    public function getName()
    {
      return $this->name;
    }
    
    public function getSurn()
    {
      return $this->surn;
    }
  }
?>

ဒီ class ရဲ့ object တစ်ခုကို ဖန်တီးကြမယ်။

php<?php
  $user = new User('john', 'smit');
?>

Variable ထဲမှာ method ရဲ့နာမည် သိမ်းထားတယ်ဆိုပါစို့။

php<?php
  $method = 'getName';
?>

Variable ထဲကနာမည်နဲ့ method ကို ခေါ်ကြည့်ရအောင်။

php<?php
  echo $user->$method(); // 'john' ကို ပြသမယ်
?>
Задание 1

အောက်ပါ class ကို ပေးထားပါတယ်။

php<?php
  class Employee
  {
    private $name;
    private $salary;
    private $position;
    
    public function __construct($name, $salary, $position)
    {
      $this->name = $name;
      $this->salary = $salary;
      $this->position = $position;
    }
    
    public function getName()
    {
      return $this->name;
    }
    
    public function getSalary()
    {
      return $this->salary;
    }
    
    public function getPosition()
    {
      return $this->position;
    }
  }
?>

Array တစ်ခု ပေးထားပါတယ်။

php<?php
  $methods = [
    'method1' => 'getName',
    'method2' => 'getSalary',
    'method3' => 'getPosition',
  ];
?>

Employee class ရဲ့ object တစ်ခုကို ဖန်တီးပါ၊ ပြီးရင် array element တွေကနေတစ်ဆင့် သူ့ရဲ့ property တွေကို ခေါ်ယူပါ။