【发布时间】:2016-08-09 04:03:24
【问题描述】:
我了解局部变量仅限于声明它们的范围,只要对象存在,实例变量就存在。但是假设我有两个类:
个人信息.php
<?php
class PersonalInformation {
private $name;
private $surname;
private $gender;
private $birthday;
function getName(){...}
function setName(){...}
//Other getter and setter
}
和 PersonalInformationController.php,它从表单中获取输入,创建 PersonalInformation 对象并设置其属性:
class PersonalInformationController {
private $personalInformation;
function __construct() {
$this->personalInformation = new PersonalInformation();
}
function doPost() {
$this->setPersonalDetails();
$this->setResidence();
$this->setContact();
}
private function setPersonalDetails() {
$name = filter_input(INPUT_POST, "name");
$surname = filter_input(INPUT_POST, "surname");
$gender = filter_input(INPUT_POST, "gender");
$birthday = filter_input(INPUT_POST, "birthday");
$nationality = filter_input(INPUT_POST, "nationality");
if (empty($name) || empty($surname)) {
throw new RequiredFieldException("Name and surname can't be empty");
} else if (!is_string($name) || !is_string($surname)) {
throw new InvalidFieldException('Input must be a string!');
} else {
$this->personalInformation->setName($name);
$this->personalInformation->setSurname($surname);
}
if (!empty($gender) && is_string($gender)) {
$this->personalInformation->setGender($gender);
} else {
throw new InvalidFieldException('Input must be a string!');
}
if (!empty($birthday) && is_string($birthday)) {
$this->personalInformation->setBirthday($birthday);
}
if (!empty($nationality) && is_string($nationality)) {
$this->personalInformation->setNationality($nationality);
}
}
private function setResidence() {
$address = filter_input(INPUT_POST, "address");
$zipCode = filter_input(INPUT_POST, "zipCode");
$city = filter_input(INPUT_POST, "city");
$nation = filter_input(INPUT_POST, "nation");
if (!empty($address) && is_string($address)) {
$this->personalInformation->setAddress($address);
}
//...//
}
private function setContact() { ... }
}
这个类有三个主要方法(setPersonalDetails() - setResidence() - setContact()),它们从表单中获取输入,在 html 页面中,将它们放入局部变量中(即 $name、$surname 等..) 并检查类型以便在 PersonalInformation 对象中设置它们。
我的问题是:“从代码设计(尤其是可扩展性和可读性)的角度来看,这些局部变量的使用或将它们声明为实例变量之间存在一些差异,以便将这三种方法仅用于检查这些变量的类型(而不是输入)?” .所以做这样的事情:
class PersonalInformationController {
private $personalInformation;
private $name;
private $surname;
private $gender;
private $birthday;
private $nationality;
private $cellphone;
//Other instance variables
function __construct() {
$this->personalInformation = new PersonalInformation();
}
function doPost() {
$name = filter_input(INPUT_POST, "name");
$surname = filter_input(INPUT_POST, "surname");
$gender = filter_input(INPUT_POST, "gender");
$birthday = filter_input(INPUT_POST, "birthday");
$nationality = filter_input(INPUT_POST, "nationality");
//...
$address = filter_input(INPUT_POST, "address");
$zipCode = filter_input(INPUT_POST, "zipCode");
$city = filter_input(INPUT_POST, "city");
$nation = filter_input(INPUT_POST, "nation");
}
private function setPersonalDetails() {
// NOW THIS METHOD ONLY CHECKS THE TYPE OF THE INPUT
if (empty($this->name) || empty($this->surname)) {
throw new RequiredFieldException("Name and surname can't be empty");
} else if (!is_string($this->name) || !is_string($this->surname)) {
throw new InvalidFieldException('Input must be a string!');
} else {
$this->personalInformation->setName($this->name);
$this->personalInformation->setSurname($this->surname);
}
if (!empty($this->gender) && is_string($this->gender)) {
$this->personalInformation->setGender($this->gender);
} else {
throw new InvalidFieldException('Input must be a string!');
}
if (!empty($this->birthday) && is_string($this->birthday)) {
$this->personalInformation->setBirthday($this->birthday);
}
if (!empty($this->nationality) && is_string($this->nationality)) {
$this->personalInformation->setNationality($this->nationality);
}
}
//setResidence() and setContact() are like the previous method.
}
【问题讨论】:
标签: php instance-variables local-variables