こんにちは私は、モデルクラスの実装が少なくとも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;
}
}
は次に実装iは
<?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にモデルを拡張すること私はを使っています。parent :: __ construct();。 私はOOPでどんな助けも私の喜びになるでしょう コメントと提案は大歓迎です。
スタックオーバーフローではなく[コードレビュー](https://codereview.stackexchange.com/)に属しているため、この質問を議論の対象外としています。 –
悲しいことに、そのようなコメントを参照してください –
あなたは良いOOPアーキテクチャを探しているなら、私はSymfonyを見ることをお勧めしたいと思います。データベースで動作するクラスが必要な場合は、クラス自体を拡張するのではなく、依存関係注入を使用することをお勧めします。 –