PHP-те ООП-де айнымалыдан әдіс атауы

Қасиет атауларына ұқсас әдіс атауларын да айнымалыда сақтауға болады. Мысалға қарайық. Бізде мынадай User класы берілген болсын, ол қасиеттерге геттерлермен:

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

Осы кластың объектісін жасайық:

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

Айнымалыда әдістің атауы сақталсын делік:

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

Айнымалыдан алынған атауы бар әдісті шалып көрейік:

php<?php
  echo $user->$method(); // шығарады 'john'
?>
Задание 1

Мынадай клас берілген:

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

Мынадай массив берілген:

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

Employee класының объектісін жасаңыз, содан кейін оның қасиеттеріне массив элементтері арқылы жүгініңіз.