Как облегчить кусок кода php?
Как облегчить этот "костыль" в функцию ?
if ($this->mother_lpr) {
$parent_surname = $this->mother_surname;
$parent_name = $this->mother_name;
$parent_middle_name = $this->mother_middle_name;
$parent_phone = $this->mother_phone;
$parent_viber = $this->mother_viber;
$parent_email = $this->mother_email;
$parent_facebook = $this->mother_facebook;
$parent_instagram = $this->mother_instagram;
}elseif($this->father_lpr){
$parent_surname = $this->father_surname;
$parent_name = $this->father_name;
$parent_middle_name = $this->father_middle_name;
$parent_phone = $this->father_phone;
$parent_vider = $this->father_vider;
$parent_email = $this->father_email;
$parent_facebook = $this->father_facebook;
$parent_instagram = $this->father_instagram;
}elseif($this->other_relative_lpr){
$parent_surname = $this->other_relative_surname;
$parent_name = $this->other_relative_name;
$parent_middle_name = $this->other_relative_middle_name;
$parent_phone = $this->other_relative_phone;
$parent_viber = $this->other_relative_viber;
$parent_email = $this->other_relative_email;
$parent_facebook = $this->other_relative_facebook;
$parent_instagram = $this->other_relative_instagram;
}
Ответы (1 шт):
Автор решения: ArchDemon
→ Ссылка
Код писал по памяти. Если будут ошибки, напишите в комментарии.
$result = [];
$attributes = ['surname', 'middle_name', 'name', 'phone'];
$base = null;
if ($this->mother_lpr) {
$base = 'mother_';
} elseif ($this->father_lpr) {
$base = 'father_';
} elseif ($this->other_relative_lpr) {
$base = 'other_relative_';
}
if ($base) {
foreach ($attributes as $a) {
$result['parent_' . $a] = $this->{$base . $a};
}
}
extract($result);