PHP DDOda Obyekt metod nomini o'zgaruvchidan olish
Xususiyat nomlarini o'zgaruvchida saqlashga o'xshab,
metod nomlarini ham saqlash mumkin. Keling, buni misol
ortaqli ko'ramiz. Bizda quyidagi User klassi
va uning xususiyat getterlari mavjud bo'lsin:
<?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;
}
}
?>
Ushbu klassning obyektini yaratamiz:
<?php
$user = new User('john', 'smit');
?>
Faraz qilaylik, o'zgaruvchida metod nomi saqlansin:
<?php
$method = 'getName';
?>
Keling, o'zgaruvchidagi nom bilan metodni chaqiramiz:
<?php
echo $user->$method(); // 'john' ni chiqaradi
?>
Quyidagi klass berilgan:
<?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;
}
}
?>
Quyidagi massiv berilgan:
<?php
$methods = [
'method1' => 'getName',
'method2' => 'getSalary',
'method3' => 'getPosition',
];
?>
Employee klassining obyektini yarating,
so'ngra uning xususiyatlariga massiv elementlari
orqali murojaat qiling.