php - Why not global $db in model classes? -


let's have mvc pattern. if have this:

blog_controller.php

<?php  class c_blog{     private $model;      public function __construct(){          include( [model_path] );          $this->model = new m_blog();     }      public function post_list(){          return $this->model->get_list();     } } 

blog_model.php

<?php  class m_blog{      public function get_list(){          global $db;          $sel = $db->query("...");          $sel = $db->fetch_array($sel);          return $sel;      } } 

index.php

<?php  $db = new database(); include "blog_controller.php"; $blog = new c_blog(); var_dump( $blog->post_list() );  ?> 

why above example isn't good, , lot of devs want oop class m_blog extends database{} ?


i want advantages , disavantages, because use "worry" method on over 15 sites high traffic , don't have problems (some of websites work multidatabases , it's easy change global $db, global $db_five.)

thanks!

why not use inheritance?

<?php  class model {     private $db;      public function __construct() {         $this->initdb();     }      private function initdb(); }  class c_blog {     private $model;      public function __construct(){          include( [model_path] );          $this->model = new m_blog();     }      public function post_list(){          return $this->model->get_list();     } }  class m_blog extends model{     public function __construct() {         parent::__construct;     }      public function get_list(){          $sel = $this->db->query("...");          $sel = $this->db->fetch_array($sel);          return $sel;      } } 

Comments