كيف يمكنني تبسيط هذا PHP؟
-
13-09-2019 - |
سؤال
لدي وظائف النموذج التالية مع PHP. أعلم أنني أكرر نفسي.
هل هناك على أي حال يمكنني تبسيط هذا الرمز؟
function getTopMenus(){
$data[0] = 'root';
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
return $data;
}
function getheadMenus(){
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
function getrootMenus(){
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
return $data;
}
المحلول
أستطيع أن أرى تبسيط واحد قد تحاول، وذلك باستخدام مرجع المرور إلى الأمور في وظيفة:
function prepareMenu(&$data) {
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[$row['id']] = $row['name'];
}
}
$Q->free_result();
}
function getTopMenus() {
$data[0] = 'root';
prepareMenus($data);
return $data;
}
function getRootMenus() {
prepareMenus($data);
return $data;
}
هناك أيضا إمكانية استخدام مرجعي و وظائف متغيرة لعدد خارج الجزء في الوسط. قد يقلل من الازدواجية، ولكن قد يعتبر أو لا يعتبر "مبسطا".
تعديل إليك ما أقصده. هذا الرمز مختبر.
function getMenus(&$data, $appendFunc) {
$this->db->where('parentid',0);
$Q = $this->db->get('menus');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$appendFunc(&$data, $row);
}
}
$Q->free_result();
}
function appendTopMenu(&$data, $row) {
$data[$row['id']] = $row['name'];
}
function appendHeadMenu(&$data, $row) {
$data[] = $row;
}
function getTopMenus() {
$data[0] = 'root';
getMenus($data, "appendTopMenu");
return $data;
}
function getheadMenus() {
getMenus($data, "appendHeadMenu");
return $data;
}
function getrootMenus() {
getMenus($data, "appendTopMenu");
return $data;
}
نصائح أخرى
لماذا لا تمر مع المعلمات في وظيفتك ووضعها في أساليب "أين" و "احصل"؟
لا أعرف كيف تبدو فئات DB واستعلامك، لكنني سأبدأ في تحسين هذه في المقام الأول. أضف "جلب الصفيف" و "جلب التجزئة" وظائف الفئة الاستعلام:
class Query ...
function as_array() {
$data = array();
if($this->num_rows() > 0)
foreach ($this->result_array() as $row)
$data[] = $row;
$this->free_result();
return $data;
}
function as_hash($key = 'id') {
$data = array();
if($this->num_rows() > 0)
foreach ($this->result_array() as $row)
$data[$row[$key]] = $row;
$this->free_result();
return $data;
}
جعل "DB-> أين ()" إرجاع نفسه
class DB
function where(...) {
stuff
return $this;
بمجرد حصولك على هذا، تصبح وظائف العميل الخاصة بك تافهة:
function getTopMenus() {
$data = $this->db->where('parentid',0)->get('menus')->as_hash();
$data[0] = 'root';
return $data;
}
function getheadMenus() {
return $this->db->where('parentid',0)->get('menus')->as_array();
}
function getrootMenus() {
return $this->db->where('parentid',0)->get('menus')->as_hash();
}
لا تنتمي إلى StackOverflow