title: Refactoring code in OOP PHP and using PDO for mysql queries
body: <p>I am new to OOP in PHP. I am having a problem while I am trying to refactor my code in OOP and PDO. The problem occurred when I tried refactor my code. So I have a category class to manage category CRUD operations. To read all the previously created categories I have a method </p>
  
  <pre><code>    public function readAll(){
          $query = "SELECT * FROM " . $this-&gt;table_name . " ORDER  BY modified ASC";
         $stmt = $this-&gt;connection-&gt;prepare($query);
           if($stmt-&gt;execute()){
              return $stmt;
            } 
    }
  </code></pre>
  
  <p>And within the same class in the construct function I am getting my db connection like so </p>
  
  <pre><code>public function __construct(PDO $db){
      $this-&gt;connection = $db;
  } 
  </code></pre>
  
  <p>and I am calling the read all method in my page like this -</p>
  
  <pre><code> $allCategories= $category-&gt;readAll();
  </code></pre>
  
  <p>and it works just fine but  when I tried to refactor my code to use a new function like this- </p>
  
  <pre><code>    private function doQuery($query){
          $stmt = $this-&gt;connection-&gt;prepare($query);
           if($stmt-&gt;execute()){
              return $stmt;
            }    
      }
      public function readAll(){
          $query = "SELECT * FROM " . $this-&gt;table_name . " ORDER  BY modified ASC";
        $this-&gt;doQuery($query);
    }
  </code></pre>
  
  <p>I am getting the following error : </p>
  
  <pre><code>Fatal error: Call to a member function fetch() on a non-object in D:\xampp\htdocs\image-upload\category.php on line 222
  </code></pre>
  
  <p>So I just want to save myself from having to write prepare and execute statements every time I make a different query. I am trying to make my code DRY. I may be missing something. A slight hint in the right direction is highly appreciated. Please help me. </p>
  
  <p>" THANKS IN ADVANCE " </p>