PHP ရှိ OOP တွင် associative array မှ property အမည်များ

ယခုမှာတော့ associative array ၏ element တစ်ခုမှ property အမည်ကိုအသုံးပြု၍ object ၏ property သို့အချက်အလက်ရယူပုံကိုကြည့်ကြပါစို့။ အောက်ပါ User class ကိုဥပမာအဖြစ်ယူထားပါမည်။

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

ဤ class ၏ object တစ်ခုကိုဖန်တီးကြပါစို့။

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

အောက်ပါ property များပါဝင်သော array တစ်ခုရှိသည်ဆိုပါစို့။

php<?php
  $props = ['prop1' => 'name', 'prop2' => 'surn'];
?>

ကျွန်ုပ်တို့၏ array ထဲရှိ element တစ်ခုတွင်သိမ်းဆည်းထားသော အမည်ကိုအသုံးပြု၍ object ၏ property သို့အချက်အလက်ရယူကြပါစို့။

php<?php
  echo $user->{$props['prop1']}; // 'john' ကိုဖော်ပြမည်
?>
Задание 1

အောက်ပါ class ကိုပေးထားပါသည်။

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

အောက်ပါ array ကိုပေးထားပါသည်။

php<?php
  $arr = [
    'prop1' => 'name',
    'prop2' => 'salary', 
    'prop3' => 'position'
  ];
?>

Employee class ၏ object တစ်ခုကိုဖန်တီးပြီး၊ array ၏ element များမှတစ်ဆင့် ၎င်း၏ properties များသို့အချက်အလက်ရယူပါ။