致命错误:在不在对象上下文中时使用$ this

时间:2016-01-12 06:24:53

标签: php

我有以下功能,但是当我运行代码时make make like: 致命错误:在E:.... 中不在对象上下文中时使用$ this 如何解决它。我替换$ this->与自我::但它也失败了。

请在这方面提供帮助,

<?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";
}
?>

1 个答案:

答案 0 :(得分:1)

您正在将$ this用于不是任何类的方法的函数。

而不是

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

使用:

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

另外,更改

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

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

以这种方式改变其余的变化。