title: Fatal error : Using $this when not in object context in
body: <p>I have the below function, But when I run the code make error like:
  <strong>Fatal error: Using $this when not in object context in E:....</strong>
  How to fix it. I replace $this-> with self:: but it failed too. </p>
  
  <p>Please help in this regards,</p>
  
  <pre><code>&lt;?php
  
  function cehck_files()
  {
      $file1 = 'C:\xampp\htdocs\test\test1.php';
      $file2 = 'C:\xampp\htdocs\test\test2.php';
      $test = $this-&gt;compareFiles($file1,$file2,true);
      $test_display = $this-&gt;toTable($test);
      echo "&lt;pre&gt;";
      print_r($test_display);
      print_r($test);
      echo "&lt;/pre&gt;";
  }
  
  function compareFiles($file1, $file2, $compareCharacters = false) {
      return $this-&gt;compare(file_get_contents($file1),file_get_contents($file2),$compareCharacters);
  }
  
  function compare($string1, $string2, $compareCharacters = false) {
  
      $start = 0;
      if ($compareCharacters){
          $sequence1 = $string1;
          $sequence2 = $string2;
          $end1 = strlen($string1) - 1;
          $end2 = strlen($string2) - 1;
      } else {
          $sequence1 = preg_split('/\R/', $string1);
          $sequence2 = preg_split('/\R/', $string2);
          $end1 = count($sequence1) - 1;
          $end2 = count($sequence2) - 1;
      }
  
          // skip any common prefix
      while ($start &lt;= $end1 &amp;&amp; $start &lt;= $end2 &amp;&amp; $sequence1[$start] == $sequence2[$start]) {
          $start ++;
      }
  
          // skip any common suffix
      while ($end1 &gt;= $start &amp;&amp; $end2 &gt;= $start &amp;&amp; $sequence1[$end1] == $sequence2[$end2]) {
          $end1 --;
          $end2 --;
      }
  
          // compute the table of longest common subsequence lengths
      $table = self::computeTable($sequence1, $sequence2, $start, $end1, $end2);
  
          // generate the partial diff
      $partialDiff =
      self::generatePartialDiff($table, $sequence1, $sequence2, $start);
  
          // generate the full diff
      $diff = array();
      for ($index = 0; $index &lt; $start; $index ++){
          $diff[] = array($sequence1[$index], UNMODIFIED);
      }
      while (count($partialDiff) &gt; 0) $diff[] = array_pop($partialDiff);
      for ($index = $end1 + 1; $index &lt; ($compareCharacters ? strlen($sequence1) : count($sequence1)); $index ++) {
          $diff[] = array($sequence1[$index], UNMODIFIED);
      }
          // return the diff
      return $diff;
  }
  
  function computeTable($sequence1, $sequence2, $start, $end1, $end2) {
      $length1 = $end1 - $start + 1;
      $length2 = $end2 - $start + 1;
  
          // initialise the table
      $table = array(array_fill(0, $length2 + 1, 0));
  
          // loop over the rows
      for ($index1 = 1; $index1 &lt;= $length1; $index1 ++) {
  
              // create the new row
          $table[$index1] = array(0);
  
              // loop over the columns
          for ($index2 = 1; $index2 &lt;= $length2; $index2 ++){
  
                  // store the longest common subsequence length
              if ($sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]) {
                  $table[$index1][$index2] = $table[$index1 - 1][$index2 - 1] + 1;
              } else {
                  $table[$index1][$index2] =
                  max($table[$index1 - 1][$index2], $table[$index1][$index2 - 1]);
              }
          }
      }
  
          // return the table
      return $table;
  }
  
  function generatePartialDiff( $table, $sequence1, $sequence2, $start ) {
      $diff = array();
  
          // initialise the indices
      $index1 = count($table) - 1;
      $index2 = count($table[0]) - 1;
  
          // loop until there are no items remaining in either sequence
      while ($index1 &gt; 0 || $index2 &gt; 0){
  
          // check what has happened to the items at these indices
          if ($index1 &gt; 0 &amp;&amp; $index2 &gt; 0 &amp;&amp; $sequence1[$index1 + $start - 1] == $sequence2[$index2 + $start - 1]) {
                  // update the diff and the indices
              $diff[] = array($sequence1[$index1 + $start - 1], UNMODIFIED);
              $index1 --;
              $index2 --;
          } elseif ($index2 &gt; 0 &amp;&amp; $table[$index1][$index2] == $table[$index1][$index2 - 1]) {
                  // update the diff and the indices
              $diff[] = array($sequence2[$index2 + $start - 1], INSERTED);
              $index2 --;
          }else{
                  // update the diff and the indices
              $diff[] = array($sequence1[$index1 + $start - 1], DELETED);
              $index1 --;
          }
      }
  
          // return the diff
      return $diff;
  }
  
  function toTable($diff, $indentation = '', $separator = '&lt;br&gt;') {
  
      $html = $indentation . "&lt;table class=\"diff\"&gt;\n";
  
          // loop over the lines in the diff
      $index = 0;
      while ($index &lt; count($diff)){
  
              // determine the line type
          switch ($diff[$index][1]){
  
                  // display the content on the left and right
              case UNMODIFIED:
              $leftCell =
              self::getCellContent(
                $diff, $indentation, $separator, $index, UNMODIFIED);
              $rightCell = $leftCell;
              break;
  
                  // display the deleted on the left and inserted content on the right
              case DELETED:
              $leftCell =
              self::getCellContent(
                $diff, $indentation, $separator, $index, DELETED);
              $rightCell =
              self::getCellContent(
                $diff, $indentation, $separator, $index, INSERTED);
              break;
  
                  // display the inserted content on the right
              case INSERTED:
              $leftCell = '';
              $rightCell =
              self::getCellContent(
                $diff, $indentation, $separator, $index, INSERTED);
              break;
          }
  
              // extend the HTML with the new row
          $html .=
          $indentation
          . "  &lt;tr&gt;\n"
          . $indentation
          . '    &lt;td class="diff'
          . ($leftCell == $rightCell
            ? 'Unmodified'
            : ($leftCell == '' ? 'Blank' : 'Deleted'))
          . '"&gt;'
          . $leftCell
          . "&lt;/td&gt;\n"
          . $indentation
          . '    &lt;td class="diff'
          . ($leftCell == $rightCell
            ? 'Unmodified'
            : ($rightCell == '' ? 'Blank' : 'Inserted'))
          . '"&gt;'
          . $rightCell
          . "&lt;/td&gt;\n"
          . $indentation
          . "  &lt;/tr&gt;\n";
  
      }
  
          // return the HTML
      return $html . $indentation . "&lt;/table&gt;\n";
  }
  ?&gt;
  </code></pre>