पीएचपी में ओओपी में वेरिएबल से मेथड का नाम

प्रॉपर्टी के नामों को वेरिएबल में स्टोर करने के अनुरूप हम मेथड के नाम भी स्टोर कर सकते हैं। आइए एक उदाहरण देखें। मान लीजिए कि हमारे पास गेटर मेथड्स वाला 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 क्लास का एक ऑब्जेक्ट बनाएं, और फिर ऐरे के एलिमेंट्स के माध्यम से इसकी प्रॉपर्टीज को एक्सेस करें।