PHP class 给出声明的未定义 public var 的错误

PHP class giving error of undefined public var that is declared

我正在为记录器编写 PHP class。我声明了两个 public 变量 $file_log$file_log_error

class Logger
{
    //constants declaration
    const FILE_BASE = '/log/comunio-uk-log-';

    // property declaration
    protected $file_log = '';
    protected $file_log_error = '';

    // Constructor
    function __construct() {
        $date = getdate();
        $file_log_name = self::FILE_BASE.$date["mday"]."-".$date["mon"]."-".$date["year"].".log";
        $file_log_error_name = self::FILE_BASE.$date["mday"]."-".$date["mon"]."-".$date["year"].".log.error";
        $this->file_log = $file_log_name;
        $this->file_log_error = $file_log_error_name;

        if (file_exists($this->file_log )) {
            echo "#OK for 'file_log' var: <br><pre>", print_r($this->file_log, true), "</pre><br><br>";
        } else {
            echo "#OK for 'file_log' var: <br><pre>", print_r($this->file_log, true), "</pre><br><br>";;
        }

        if (file_exists($this->file_log_error)) {
            echo "#ERROR for 'file_log_error' var: <br><pre>", print_r($this->$file_log_error, true), "</pre><br><br>";
        } else {
            echo "#ERROR for 'file_log_error' var: <br><pre>", print_r($this->$file_log_error, true), "</pre><br><br>";
        }
    }
}

当我创建一个新的 class 对象时,构造函数被调用并且我收到以下错误:

#OK for 'file_log': 
/log/comunio-uk-log-13-2-2016.log

#ERROR for 'file_log_error': 

Notice:  Undefined variable: file_log_error in C:\xampp5.6\htdocs\comuniazo-uk\api\bd\logger.php on line 28

Fatal error:  Cannot access empty property in C:\xampp5.6\htdocs\comuniazo-uk\api\bd\logger.php on line 28

为什么第一个受保护的变量 $file_log 被识别而第二个 $file_log_error 被识别,不是吗?

我已经尝试将变量声明为 public 和私有。结果相同。

$file_log_error 从未定义。替换

print_r($this->$file_log_error, true)

print_r($this->file_log_error, true)

如果你写

$varName = 'test';
$obj = new stdClass;
$obj->test = 'foo';
$obj->varName = 'bar';
echo $obj->$varName; // echo $obj->test gives 'foo', not 'bar'