-1

I've recently been working on a PHP "tail -f" type of functionality that prints changes to a file to a web, but can't seem to get it to work due to the following PHP errors:

PHP Notice: Undefined variable: tail

PHP Fatal error: Call to a member function tailCmd() on a non-object

My PHP code is as follows:

<?php
class PHPTail {

    private $logfile = "";
    private $updateTime;
    private $maxSizeToLoad;

    public function __construct($logfile, $defaultUpdateTime = 1000, $maxSizeToLoad = 1048576) {
        $this->log = $logfile;
        $this->updateTime = $defaultUpdateTime;
        $this->maxSizeToLoad = $maxSizeToLoad;
    }

    public function getNewLines($lastFetchedSize, $filters) {
        // some code
    }

    public function tailCmd() {
        // some code
    }
}

if(isset($_GET['var'])) {
    $var = $_GET['var'];
    if(file_exists("logfile-".$var.".txt")) {
        $file = "logfile-".$var.".txt";
    }
    $tail = new PHPTail($file);
}

if(isset($_GET['filters'])) {
    $filters = $_GET['filters'];
}
else {
    $filters = "";
}

if (isset($_GET['ajax'])) {
    echo $tail->getNewLines($_GET['lastFetchedSizesize'], $filters);
    die();
}

$tail->tailCmd();
?>

It seems like the echo $tail->getNewLines($_GET['lastFetchedSizesize'], $filters); command seems to be failing when I assign the $var variable the value from $_GET['var'];, which is a dynamically assigned number from the URL in string format, then I concatenate $var into a file location and filename string like so: $file = "logfile-$var.txt"; or $file = "logfile-".$var.".txt";. That's when I get the above PHP errors, the first of which referring to the echo $tail->getNewLines command.

However, everything seems to work fine if I make the $file variable equal to a non-concatenated string value like so: $file = "logfile-1.txt";.

I'm pretty new to PHP OOP, so I'm guessing that I'm doing something that is illegal, but I so far haven't been able to figure out what that is. I'm thinking that I have some sort of syntax issue at the new Class instantiation $tail = new PHPTail($file);.

yivi
  • 42,438
  • 18
  • 116
  • 138
Zallus
  • 1
  • 3
  • This has nothing to do with OOP. You only set `$tail` when `$_GET['var']` is set. If the variable isn't set, you'll get these errors. – Barmar Feb 13 '20 at 01:35
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – yivi Feb 13 '20 at 08:33

2 Answers2

2

When $_GET['var'] isn't set, you never set $tail. You need to move all the code that depends on this into the if statement.

if(isset($_GET['var'])) {
    $var = $_GET['var'];
    if(file_exists("logfile-".$var.".txt")) {
        $file = "logfile-".$var.".txt";
    }
    $tail = new PHPTail($file);

    if(isset($_GET['filters'])) {
        $filters = $_GET['filters'];
    }
    else {
        $filters = "";
    }

    if (isset($_GET['ajax'])) {
        echo $tail->getNewLines($_GET['lastFetchedSizesize'], $filters);
        die();
    }

    $tail->tailCmd();
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

When $_GET['var'] isn't set, your code will never set $tail. You need to move all the code that depends on that variable into the if statement.

Try this first var_dump $_GET;

jalsh
  • 801
  • 6
  • 18
Alan Wang
  • 41
  • 5