title: Object oriented CRUD with Mysqli
body: <p>I'm trying to learn OOP programming in PHP. So far i got couple of basic things but i don't know how to echo my results into the page. I made up Database class and Category class i wanna echo the results from read function but i don't know how to do that. Can someone help me refactor this so i can use both classes?</p>
  
  <p><strong>This is my code</strong></p>
  
  <pre><code>class Database {
  private $_connection;
  private $_host = "localhost";
  private $_username = "root";
  private $_password = "";
  private $_database = "cmsi";
  
  // Conect to database is private and can only be used by getConection function result is returned object of mysqli class
  //can be seen in var_dump() function
  private function conect() {
      //$this refers to class Database and her functions and propertys are being acsessed via -&gt; sign
      $this-&gt;_connection = new mysqli($this-&gt;_host, $this-&gt;_username,
                                      $this-&gt;_password, $this-&gt;_database);
  
      // Error handling
      if(mysqli_connect_error()) {
          trigger_error("Failed to conencto to MySQL: " . mysql_connect_error(),
                        E_USER_ERROR);
      }
      return $this-&gt;_connection;
  
  }
  //Safest way to iniate conection
  public function getConection(){
      return $this -&gt; conect();
  }
  }
  
  
   class Category{
  private $db_sql;
  public function  __construct($db){
      $this-&gt;db_sql = $db;
  }
  
  public function read(){
      $conection = $this-&gt;db_sql;
      $query = "Select * from category";
      $result = $conection -&gt; query($query);
      return $result;
     }
  
   }
  
  $db = new Database();
  $conection = $db-&gt;getConection();
  $obj = new Category($conection);
  $obj-&gt;read();
  </code></pre>