Home » Php » PHP OOP "tail -f" functionality failing

PHP OOP "tail -f" functionality failing

Posted by: admin February 25, 2020 Leave a comment

Questions:

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);.

How to&Answers:

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();
}

Answer:

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;