0

I have the below function, But when I run the code make error like: Fatal error: Using $this when not in object context in E:.... How to fix it. I replace $this-> with self:: but it failed too.

Please help in this regards,

<?php

function cehck_files()
{
    $file1 = 'C:\xampp\htdocs\test\test1.php';
    $file2 = 'C:\xampp\htdocs\test\test2.php';
    $test = $this->compareFiles($file1,$file2,true);
    $test_display = $this->toTable($test);
    echo "<pre>";
    print_r($test_display);
    print_r($test);
    echo "</pre>";
}

function compareFiles($file1, $file2, $compareCharacters = false) {
    return $this->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 <= $end1 && $start <= $end2 && $sequence1[$start] == $sequence2[$start]) {
        $start ++;
    }

        // skip any common suffix
    while ($end1 >= $start && $end2 >= $start && $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 < $start; $index ++){
        $diff[] = array($sequence1[$index], UNMODIFIED);
    }
    while (count($partialDiff) > 0) $diff[] = array_pop($partialDiff);
    for ($index = $end1 + 1; $index < ($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 <= $length1; $index1 ++) {

            // create the new row
        $table[$index1] = array(0);

            // loop over the columns
        for ($index2 = 1; $index2 <= $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 > 0 || $index2 > 0){

        // check what has happened to the items at these indices
        if ($index1 > 0 && $index2 > 0 && $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 > 0 && $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 = '<br>') {

    $html = $indentation . "<table class=\"diff\">\n";

        // loop over the lines in the diff
    $index = 0;
    while ($index < 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
        . "  <tr>\n"
        . $indentation
        . '    <td class="diff'
        . ($leftCell == $rightCell
          ? 'Unmodified'
          : ($leftCell == '' ? 'Blank' : 'Deleted'))
        . '">'
        . $leftCell
        . "</td>\n"
        . $indentation
        . '    <td class="diff'
        . ($leftCell == $rightCell
          ? 'Unmodified'
          : ($rightCell == '' ? 'Blank' : 'Inserted'))
        . '">'
        . $rightCell
        . "</td>\n"
        . $indentation
        . "  </tr>\n";

    }

        // return the HTML
    return $html . $indentation . "</table>\n";
}
?>
Saleem Jafar
  • 51
  • 5
  • 13
  • The question has already an answer [$this when not in object context](http://stackoverflow.com/questions/34523304/this-when-not-in-objects-context/34523765#34523765) – Basheer Kharoti Jan 12 '16 at 06:25
  • 2
    your functions are not inside a `class`? if so, you don't need to use `$this` – roullie Jan 12 '16 at 06:26
  • Not seeing any `Class` there? try without `$this`. – Sougata Bose Jan 12 '16 at 06:27
  • You're simply making `functions` and you're not writing those `function` within scope of `Class` so you can't use `$this` and `self` simply use `getCellContent(` function instead of `$this` or `self` – Narendrasingh Sisodia Jan 12 '16 at 06:29
  • at which line you are getting error ? – Monty Jan 12 '16 at 06:56
  • I think something is wrong with these lines of code: case DELETED: $leftCell = getCellContent( $diff, $indentation, $separator, $index, DELETED); $rightCell = getCellContent( $diff, $indentation, $separator, $index, INSERTED); break; The error is : Fatal error: Call to undefined function getCellContent() in.... – Saleem Jafar Jan 12 '16 at 07:04
  • it seems copy-paste to me.. –  Jan 12 '16 at 07:26

1 Answers1

1

You are using $this for a function which is not a method of any class.

Instead of

$test = $this->compareFiles($file1,$file2,true);

Use:

$test = compareFiles($file1,$file2,true);

Also, change

return $this->compare(file_get_contents($file1),file_get_contents($file2),$compareCharacters);

To

return compare(file_get_contents($file1),file_get_contents($file2),$compareCharacters);

And to the remaining changes in this way.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • Can anyone run this code in your end and fix the issues please...... It will really help me and your help in this regards will be really appreciated – Saleem Jafar Jan 12 '16 at 07:05