title: do I have to check login session when initiating PHP class?
body: <p>I used to program php in procedural way. Since I would like to learn more about OOP I have decided to program php in OOP way for my new project.</p>
  
  <p>Anyways, let's say I have a project that requires user to login. Which means in login.php once user enters the correct username and password, it will be redirected to index.php and will start loading all the products from the product table and display them nicely in index.php.</p>
  
  <p>Before, how I did this was, in login.php I'll have the following code:</p>
  
  <p><strong>login.php</strong></p>
  
  <pre><code>session_start();
  ...
  if (loggedCorrect($user, $password)) {
       $_SESSION['loggedinuser'] = $user;
       //redirect to index.php
  }
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>session_start();
  if (isset($_SESSION['loggedinuser']) {
        //select fields from products table and display them
        ...
  }
  </code></pre>
  
  <p>so in OOP it will be something like:</p>
  
  <p><strong>login.php</strong></p>
  
  <pre><code>session_start();
  $user = new User($user, $password);
  if ($user-&gt;hasCorrectLogin()) {
       $_SESSION['loggedinuser'] = $user-&gt;getUsername();
       //redirect to index.php
  }
  </code></pre>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>session_start();
  if (isset($_SESSION['loggedinuser']) {
       $products = new Products();
       //display all products
  }
  </code></pre>
  
  <p><strong>Product class</strong></p>
  
  <pre><code>class Products {
      private $productArray;
      ...
      __construct() {
           //select all products from mySQL table then put every product in productArray
      }
  ...
  }
  </code></pre>
  
  <p>My question are:</p>
  
  <ol>
  <li><p>when initiating object (like Products in my case). do I have to check login session? if so, should I do it inside __contruct? or should I do it before the "class Products" line?</p></li>
  <li><p>I also have a cronjob.php, which will be executed every x minutes. When it's executing, it will create some objects like Products and analysis them. So if login session checking is required, then I'm not sure how to make this works, as cronjob doesn't support session. </p></li>
  </ol>
  
  <p>Please advise</p>