title: Should isset checks be done within a service that depends on a session being valid?
body: <p>I am working on a <code>UserSession</code> service, the code is below.</p>
  
  <p>My question is, is there any need to do an <code>isset</code> test within the <code>get_id</code> method or should it be the responsibility of the calling code (in my case the controller) to ensure a session is active?</p>
  
  <p>If the session isn't set, a failure would result in a logout, but I can't help but think that checking for the validity of a user session shouldn't be done in this service, as the use of this service requires a valid user session anyway.</p>
  
  <p>I end up having to do this in every getter.</p>
  
  <pre><code>&lt;?php
  
  namespace \App\Services;
  
  class UserSession {
  
      private $session;
  
      public function __construct()
      {
          $this-&gt;session = (object) $_SESSION;
      }
  
      public function get_id()
      {
          if(!isset($this-&gt;session-&gt;user-&gt;id))
              return false;
  
          return $this-&gt;session-&gt;user-&gt;id;
      }
  }
  </code></pre>
  
  <p>Any advice?</p>
  
  <p>I'm thinking maybe a method <code>is_valid()</code> that returns <code>true</code> or <code>false</code> is maybe needed. That way the use of the session can be checked prior to calling any of the getters.</p>