PHP'de OOP'de Değişkenden Metot İsmi Kullanma

Özellik isimlerinin değişkende saklanmasına benzer şekilde metot isimleri de saklanabilir. Bir örnek üzerinden görelim. Aşağıdaki gibi User sınıfı ve özellik getter'ları verilsin:

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;
    }
  }
?>

Bu sınıfın bir nesnesini oluşturalım:

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

Değişkende bir metot isminin saklandığını varsayalım:

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

Değişkenden gelen isme sahip metodu çağıralım:

php<?php
  echo $user->$method(); // 'john' çıktısını verir
?>
Задание 1

Aşağıdaki sınıf verilsin:

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;
    }
  }
?>

Aşağıdaki dizi verilsin:

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

Employee sınıfından bir nesne oluşturun ve ardından özelliklerine dizi elemanları aracılığıyla erişin.