PHP-dä OOP-da beýleki obýektiň häsiýet ady
Häsiýetiň ady, hatda beýleki obýektiň
häsiýeti hem bolup biler. Bizde aşakdaky User klassy bar diýeliň:
<?php
class User
{
public $name;
public $surn;
public function __construct($name, $surn)
{
$this->name = $name;
$this->surn = $surn;
}
}
?>
Bu klassyň obýektini döredeliň:
<?php
$user = new User('john', 'smit');
?>
Bizde şeýle hem Prop klassy
bar diýeliň, onuň value
häsiýetinde häsiýetiň ady saklanar:
<?php
class Prop
{
public $value;
public function __construct($value)
{
$this->value = $value;
}
}
?>
Bu klassyň obýektini döredeliň:
<?php
$prop = new Prop('name');
?>
Indi bu obýektiň kömegi bilen ulanyjynyň adyny çykaralyň:
<?php
echo $user->{$prop->value}; // 'john' çykarar
?>
Aşakdaky klas berlen:
<?php
class Employee
{
public $name;
public $salary;
public $position;
public function __construct($name, $salary, $position)
{
$this->name = $name;
$this->salary = $salary;
$this->position = $position;
}
}
?>
Şeýle hem bu klas berlen:
<?php
class Data
{
public $prop1 = 'name';
public $prop2 = 'salary';
public $prop3 = 'position';
}
?>
Employee obýektiniň häsiýetlerini
Data obýekti arkaly çykaryň.