PHP හි OOP හි විචල්යයකින් ක්රම නාමය
ගුණාංග නාමයන් සමඟ සදෘශ්යයෙන්,
ක්රමයන්ගේ නම් ද විචල්යයක ගබඩා කළ හැකිය.
අපි උදාහරණයක් බලමු. අපට පහත දක්වා ඇති
ගුණාංග ගෙටර් සමඟ User පන්තිය ඇතැයි සිතමු:
<?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
$user = new User('john', 'smit');
?>
විචල්යයක ක්රමයේ නම ගබඩා වී ඇතැයි සිතමු:
<?php
$method = 'getName';
?>
විචල්යයේ ඇති නම සමඟ ක්රමය කැඳවමු:
<?php
echo $user->$method(); // 'john' ප්රතිදානය කරයි
?>
පහත පන්තිය ලබා දී ඇත:
<?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
$methods = [
'method1' => 'getName',
'method2' => 'getSalary',
'method3' => 'getPosition',
];
?>
Employee පන්තියේ වස්තුවක් නිර්මාණය කර,
ඉන්පසු එහි ගුණාංග වෙත රාශියේ මූලද්රව්ය
හරහා ආකේශනය කරන්න.