title: How to execute SQL Query in seperate class function of Codeigniter
body: <p>I am new on CodeIgniter. I want to execute all database queries in a separate class function just for the sake of reuse-ability. <strong><em>Same like as in JAVA spring framework.</em></strong></p>
  
  <p><strong>Example: i need the List of all users in different controllers. for this purpose wrote the query for getAllUser() in separate Class Function. After that i can call it from different Controllers</strong></p>
  
  <p>This is the piece of code that i tried to achieve my goal</p>
  
  <p>It will be my service class</p>
  
  <pre><code>class UserManagementServices extends CI_Controller {   
      public static function getAllUsers(){
          $this-&gt;load-&gt;database();
          return $this-&gt;db-&gt;query("SELECT * FROM users");
      }
      public static function getUser($userId){ ... }
      public static function removeUser($userId){ ... }
      public static function getUserHistory($userId){ ... }
  }
  </code></pre>
  
  <p>It will be my controller class</p>
  
  <pre><code>class User extends CI_Controller {
      public function index(){
          $this-&gt;load-&gt;view('userHome');
      }
      public function viewAllUser(){
          include 'application/controllers/UserManagementService.php';
          var_dump(UserManagementService::getAllUsers());
      }
  }
  </code></pre>
  
  <p>But this piece of code is not working. </p>
  
  <p><strong>In CodeIgniter It is not allowing me to execute the query in static method.</strong> 
  When i tried this</p>
  
  <pre><code>class UserManagementServices extends CI_Controller {   
      public static function getAllUsers(){
          return 'Testing Purpose...';
      }
  }
  </code></pre>
  
  <p>and it works fine.</p>