title: PHP MVC Best Practice - Pass Session variable to model class from controller or access directly in model
body: <p>Our development team is debating a general best practice:
  Is it better to access a session variable directly from a function in a model class or pass the session variable from the controller as an argument to a function in the model class.  Look at the two examples below:</p>
  
  <p>Accessing session variable directly from the model class to use in a query:</p>
  
  <pre><code>class MyModel {
      public function getUserPrefs($userID) {
          $this-&gt;query("SELECT * FROM my_table WHERE id=$_SESSION['userID']");
      }
  }
  </code></pre>
  
  <p>Or pass the session variable from the controller to a function in the model class as a function argument:</p>
  
  <pre><code>class MyController {
      public function displayUsers() {
          $this-&gt;model-&gt;getUserPrefs($_SESSION['userID']);
      }
  }
  
  class MyModel {
      public function getUserPrefs($userID) {
          $this-&gt;query("SELECT * FROM my_table WHERE id=$userID");
      }
  }
  </code></pre>
  
  <p>The reasoning for passing it to the model from the controller is so all data that is referenced is coming from one point of entry, that being the controller.</p>
  
  <p>What is recognized as a better practice?</p>