title: Getting error using $this when not in object context to all methods in a class except __construct() method
body: <p>I have two classes in separate files. The names of classes and files are: Substitute.php and Models.php. I have some properties in class Substitute.php which I can access using <code>$this-&gt;property_name</code> in the <code>__construct()</code> method without any error. However when I try to access the <strong>same property</strong> in the <strong>another method</strong>(method <code>test()</code> in my case(see the code below)) in the <strong>same class</strong> (i.e., class Substitute) I get the error Fatal error: <em>Uncaught Error: Using $this when not in object context in G:\ROHAN\eTh0\VectorVolunteers\vector\backend\models\Substitute.php:22</em>. Line 22 being <code>echo $this-&gt;c_day;</code> in the method <code>sss()</code> (I have cut some of the code in the class, adding only the which I thought to be relevant, I'll add rest of the code in case someone requires). I access methods <code>sss()</code> and <code>test()</code> from the url by <code>autoloader method</code> and <code>class Router</code> which I have included below too:
  Below this line lies the code inside Substitute.php:
  EDIT1: I have added another method from Substitute class where also I get the same error.</p>
  
  <pre><code>class Substitute extends Models{
      public $user_id, $c_day, $c_date;
      public function __construct(){
          $_SESSION['username']='user2';
          echo 'constructor in class Substitute ran';
          $this-&gt;user_id = $this-&gt;get_user_id();
          $this-&gt;c_day = $this-&gt;upcoming_class();
      }
      public function sss(){
          echo $this-&gt;c_day;
      }
      public function test(){        
          if(isset($_POST['select_class_subs'])){
              switch ($_POST['select_class_subs']) {
                  case 'Next Class':
                      $this-&gt;c_date = date('d-M-yy', strtotime(($this-&gt;c_day)));
                      break;
  
                  case 'Next-to-next Class':
                      $this-&gt;c_date = date('d-M-yy', strtotime(($this-&gt;c_day . '+1 week')));
                      break;
  
                  default:
                      echo 'Custom class selected &lt;br&gt;';
                      break;
              }
              if($this-&gt;c_date==date('d-M-yy')){
                  echo "Sorry, but you cannot post a substitute request for a class that is scheduled to happen on the same day. &lt;br&gt;";
              }
              else{
                  echo "Done! &lt;br&gt;";
              }
          }
      }
  </code></pre>
  
  <p>EDIT2: The code in the file <em>config.php</em> which is required in the <code>autoloader()</code> method(See below):</p>
  
  <pre><code>if(isset($_GET['url'])){$url = explode('/', $_GET['url']);}
  require_once (ROOT . DS . 'backend' . DS . 'bootstrap.php');
  </code></pre>
  
  <p>EDIT2: The code of file <em>bootstrap.php</em> which has <code>autloader()</code> method:</p>
  
  <pre><code>require_once 'config.php';
  
  // Autoloader for classes
  spl_autoload_register('autoloader');
  function autoloader($class_Name){
      if (file_exists (ROOT . DS . 'backend' . DS .'core' . DS . $class_Name . '.php')){
          require_once (ROOT . DS . 'backend' . DS . 'core' . DS . $class_Name . '.php');
      }
      elseif (file_exists (ROOT . DS . 'backend' . DS .'models' . DS . $class_Name . '.php')){
          require_once (ROOT . DS . 'backend' . DS . 'models' . DS . $class_Name . '.php');
      }
      elseif (file_exists(ROOT . DS . 'backend' . DS .'views' . DS . $class_Name . '.php')){
          require_once (ROOT . DS . 'backend' . DS . 'views' . DS . $class_Name . '.php');
      }
      elseif (file_exists(ROOT . DS . 'backend' . DS .'controllers' . DS . $class_Name . '.php')){
          require_once (ROOT . DS . 'backend' . DS . 'controllers' . DS . $class_Name . '.php');
      }
      elseif (file_exists(ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php')) {
          require_once (ROOT . DS . 'backend' . DS .'database' . DS . $class_Name . '.php');
      }
      elseif (file_exists(ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php')) {
          require_once (ROOT . DS . 'backend' . DS .'reminders' . DS . $class_Name . '.php');
      }
      else {echo 'Class does not exist or Class file not found' . '&lt;br&gt;';}
  }
  
  // Route the request to router.php
  if (isset($_GET['url'])) {
  Router::route($url);
  }
  </code></pre>
  
  <p>EDIT2: Code in the file that contains <code>class Router{}</code>:</p>
  
  <pre><code>class Router{
    private static $controller_name, $method_name;
    public static function route($url){
        if (isset($url[0]) &amp;&amp; $url[0] != "") {
          $controller = ucwords($url[0]);
          self::$controller_name = $controller;
          array_shift($url);
        }
        if (isset($url[0]) &amp;&amp; $url[0] != "") {
          $method =  ucwords($url[0]);
          self::$method_name = $method;
          array_shift($url);
        }
  
        $params = $url;
  
        $init = new self::$controller_name;
  
        if (method_exists(self::$controller_name, self::$method_name)){
            call_user_func_array([self::$controller_name, self::$method_name], $params);
        }
        else{
          if(self::$method_name!=NULL){
            echo 'The requested method "' . self::$method_name . '" does not exist in ' . '"' . self::$controller_name . '" controller.';
          }
        }
    }
  </code></pre>
  
  <p>Below this lies the code inside Models.php (I have included it in case someone requires. But know that I have extended this class to many other classes and all of the methods in this class work fine without any errors or warning).</p>
  
  <pre><code>class Models extends Database {
  private $db, $user_id, $table, $rv_id_table_name, $av_id_table_name, $class_day;
      protected function get_user_id(){
          if (isset($_SESSION['username'])){
              $username=$_SESSION['username'];
              $sql = 'SELECT * FROM volunteers where username=:username';
              $params = [
                  'username' =&gt; $username
              ];
              $array = $this-&gt;query($sql, $params);
              $this-&gt;user_id = $array['id'];
              return $this-&gt;user_id;
          }
          else{
              echo 'You are not signed in.' . '&lt;br&gt;' . 'Please sign in first.';
          }
      }
      protected function upcoming_class(){
          if (isset($_SESSION['username'])) {
              $id = $this-&gt;get_user_id();
              $params = [
                  'id' =&gt; $id
              ];
              $sql = 'SELECT class_day FROM volunteers where id=:id';
              $this-&gt;class_day = $this-&gt;query($sql, $params);
              return $this-&gt;class_day['class_day'];
          }
      }
  </code></pre>
  
  <p>I want to know why am I getting the above mentioned error (in Italics) and how to remove it. I asked it because I have been trying since yesterday, with no success.</p>