Meetodi nimi muutujast OOP-s PHP-s

Sarnaselt omaduste nimedega muutujas saab salvestada ka meetodite nimesid. Vaatame seda näite varal. Olgu meil antud järgmine klass User koos omaduste getteritega:

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

Loome selle klassi objekti:

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

Olgus muutujas salvestatud meetodi nimi:

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

Kutsume välja meetodi nimega muutujast:

php<?php
  echo $user->$method(); // väljastab 'john'
?>
Задание 1

Antud on järgmine klass:

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

Antud on massiiv:

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

Looge objekt klassist Employee, ning seejärel pööruge selle omaduste poole läbi massiivi elementide.