title: Class with a db query, extending, functions and views. Am I doing it right way?
body: <p>I have just a little progress in practicing. Most of my code works but I am not sure if I do things the right way?</p>
  
  <p>Please, can you tell me if I do mistakes and correct me.</p>
  
  <p><strong>First, I create autoload functions:</strong></p>
  
  <pre><code>function autoload_models($model) {
      if (file_exists(MODELS_PATH . $model . '.php')) {
          require_once MODELS_PATH . $model . '.php';
          return true;
      } else {
          return false;
      }
  }
  
  spl_autoload_register('autoload_models');
  
  
  function autoload_controllers($controller) {
      if (file_exists(CONTROLLERS_PATH . $controller . '.php')) {
          require_once CONTROLLERS_PATH . $controller . '.php';
          return true;
      } else {
          return false;
      }
  }
  
  spl_autoload_register('autoload_controllers');
  </code></pre>
  
  <p><strong>I have a class like this:</strong></p>
  
  <pre><code>class Category {
  
      public $db;
      public $rows;
      public $id;
  
      public function build_category() {
  
          global $db;
          global $rows;
  
          $db = new Database();
          $db-&gt;query("SELECT * from categories");
          $rows = $db-&gt;resultset();
      }
  
      public function category_items() {
  
          global $db;
          global $rows;
          global $id;
  
          $db = new Database();
          $db-&gt;query("SELECT * from posts WHERE category_id = '$id'");
          $rows = $db-&gt;resultset();
      }
  
  }
  </code></pre>
  
  <p><strong>I extend with another class</strong> <em>(still have some issues here. <strong>Nothing</strong> prints)</em>:</p>
  
  <pre><code>class Category_Items extends Category {
  
      public $db;
      public $rows;
      public $id;
  
      public function display_category_items() {
  
          // Call the parent class function
          parent::category_items();
  
          global $rows;
          global $id;
  
  
          // Check if the page parameter is integer
          if (ctype_digit($_GET['id'])) {
              $id = $_GET['id'];
          } else {
              print "Illegal category page parameter";
          }
  
  
          foreach ($rows as $row) {
  
              print "test";
              print $row['post_title']; // This does not work yet. Nothing prints
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Class for building a menu with categories</strong> <em>(Everything works here)</em>:</p>
  
  <pre><code>class Categories_Menu extends Category {
  
      public $db;
      public $rows;
  
      public function build_category_menu() {
  
          parent::build_category();
  
          global $rows;
  
          foreach ($rows as $row) {
  
              require VIEWS_PATH . 'categories/categories_menu.php';
          }
      }
  
  }
  </code></pre>
  
  <p>And finally instances:</p>
  
  <pre><code>$category_menu = new Categories_Menu();
  $category_menu-&gt;build_category_menu();
  
  $category_items = new Category_Items();
  $category_items-&gt;display_category_items();
  </code></pre>
  
  <p>Thank you for your time and help!</p>