재사용 가능성을 극대화하기 위해 "복사"및 "붙여 넣기"를 피할 수 있도록 두 클래스를 디자인하는 방법에 몇 가지 문제가 있습니다. 사용자를 생성하는 클래스가 있습니다. 본질적으로 항목을 확인한 다음 사용자를 데이터베이스에 삽입합니다. 반면에, 나는 사용자를 업데이트하는 또 다른 클래스를 가지고 있지만, 사용자 프로파일의 일부분만을 가지고있다. 제 첫 수업이 있습니다.PHP 클래스 상속 및 데이터 유효성 검사
<?php
class newEmployee extends CoreConnect {
protected $_info = array();
protected $_errors = array();
public function __construct($data) {
parent::__construct();
$this->_info = array(
'firstname' => '',
'lastname' => '',
'street' => '',
'postal-code' => '',
'province' => '',
'email' => '',
'country' => '',
'userUpdate' => '',
'wage' => 0,
'emp-status' => '');
$this->_info = $data + $this->_info;
}
protected function validate() {
if($this->_info['firstname']=="" || $this->_info['lastname']==""){
$this->_errors[] = "Employee's full name is required";
}
if(ctype_alpha ($this->_info['firstname'].$this->_info['lastname'])==false) {
$this->_errors[] = "Employee name is invalid";
}
if($this->_info['wage']!="") {
if(!is_numeric($this->_info['wage'])) {
$this->_errors[] = "Salary invalid";
}
}
if($this->_info['userUpdate']=="" && isset($this->_info['userUpdate'])) {
$this->_errors[] = "User missing";
}
if($this->_info['email']!="") {
if (!filter_var($this->_info['email'], FILTER_VALIDATE_EMAIL)) {
$this->_errors[] = "Email address is invalid";
}
}
}
public function errors() {
return $this->_errors;
}
public function add(){
$this->validate();
if(empty($this->_errors)) {
$params = array();
$query = $this->_INSTANCE->pdo->prepare("INSERT INTO employees
(ID, firstname, lastname, email, street, postal_code, province, country, employee_type, member_since)
VALUES (NULL, :firstname, :lastname, :email, :street, :postal_code, :province, :country, :status, NOW())
");
$params[":firstname"] = $this->_info["firstname"];
$params[":lastname"] = $this->_info["lastname"];
$params[":street"] = $this->_info["street"];
$params[":email"] = $this->_info["email"];
$params[":postal_code"] = $this->_info["postal-code"];
$params[":province"] = $this->_info["province"];
$params[":country"] = $this->_info["country"];
$params[":status"] = $this->_info["emp-status"];
return $query->execute($params);
}
}
}
여기서 validate
메서드는 유효성을 검사 할 항목을 확인합니다. 사용자의 데이터를 업데이트 클래스는 다음과 같습니다 :이 경우
<?php
require_once 'class.newEmployee.php';
class updateEmployee extends newEmployee {
public function __construct($data) {
parent::__construct();
$this->_info = array(
'employeeID' => "",
'firstname' => '',
'lastname' => '',
'email' => '',
'street' => '',
'postal-code' => '',
'province' => '',
'country' => '');
$this->_info = $data + $this->_info;
}
public function update(){
$this->validate();
if(empty($this->_errors)) {
$params = array();
$query = $this->_INSTANCE->pdo->prepare("UPDATE employees
SET
firstname=:firstname,
lastname=:lastname,
street = :street,
postal_code = :postal_code,
province = :province,
email = :email,
country = :country
WHERE ID = :ID ");
$params[":ID"] = $this->_info["employeeID"];
$params[":firstname"] = $this->_info["firstname"];
$params[":lastname"] = $this->_info["lastname"];
$params[":street"] = $this->_info["street"];
$params[":postal_code"] = $this->_info["postal-code"];
$params[":province"] = $this->_info["province"];
$params[":email"] = $this->_info["email"];
$params[":country"] = $this->_info["country"];
return $query->execute($params);
} else {
return false;
}
}
}
내가 validate
방법은 newEmployee
에서 유효성을 검사뿐만 아니라 employeeID
라는 새로운 입력뿐만 아니라 어떤 유효성을 검사 할. 기본적으로 달성하고자하는 것은 어떻게 든 부모 클래스의 일부를 하위 클래스에 사용하고 새 유효성 검사를 가져올 수있게하는 것입니다. 다른 유효성 검사 지점을 추가하기 위해 validate
메서드를 재정의하는 것은 분명히 불편합니다. 새 직원을 만드는 데 사용 된 것과 동일한 유효성 검사를 수행하고 직원을 업데이트하는 것이 좋습니다. 코드 재사용 가능성은 직원을 만들거나 프로필을 업데이트 할 때마다 동일해야하기 때문에이 경우 필수 사항입니다.
이 질문은 [코드 검토]에 속하는를 (http://codereview.stackexchange.com/). –