안녕하세요, 저는 모델 클래스의 구현이 적어도 OOP 스타일에 맞는지 물어보고 싶습니다. 그것은 단지 클래스를 확장에 사용할 수 있도록 나는 추상로 선언PHP에서 모델 클래스의 적절한 구현
샘플 코드 :
는<?php
/**
* class model handles dbconfig and some common query transaction
* i declare it as an abstract so that it can only be used on extending the
*class
*/
abstract class Model
{
//db config
protected $sHost = "localhost";
protected $sUser = "root";
protected $sPass = " ";
protected $sDb = "test";
protected $sEngine = "MySQL";
protected $conn ;
//constructor
public function __construct()
{
$this->conn = new mysqli($this->sHost, $this->sUser, $this->sPass , $this->sDb);
}
protected function db_query_list($sSql){
if ($resultset = $this->conn->query($sSql)) {
if ($resultset->num_rows > 0) {
$data = array();
while($row = $resultset->fetch_assoc()) {
$data[] = array_change_key_case($row);
}
}else {
$data = false;
}
} else {
$data = false;
}
$resultset->close();
return $data;
}
protected function execute_query($SQL) {
$run = $this->conn->query($this->sEngine);
return $run;
}
}
그런 다음 구현에 내가
<?php
require "Model.php";
class CustomerModel extends Model
{
public function __construct()
{
parent::__construct();
}
public function getAllCustomer()
{
$sSql = "SELECT *
FROM t_classification_header
";
return $this->db_query_list($sSql);
}
}
공지 사항 CustomerModel에서 모델을 확장하는 나는 부모를 사용한다 :: __ construct();. OOP에 새로운 사람이라면 도움이 될 것입니다. 의견이나 제안을 환영합니다.
스택 오버플로가 아니라 [코드 검토] (https://codereview.stackexchange.com/)에 속하기 때문에이 질문을 주제와 관련이없는 것으로 닫으려고합니다. –
슬픈 댓글을 보시려면 –
좋은 OOP 아키텍처를 찾고 계신다면 Symfony를 살펴 보시기 바랍니다. 데이터베이스에서 작동하는 클래스가 필요하면 클래스 자체를 확장하는 대신 종속성 삽입을 사용하는 것이 좋습니다. –