2016-02-13 92 views
0

我正在写一个记录器的PHP类。我已经宣布了两个公共变数$file_log$file_log_errorPHP类给出错误的未定义公共变种,宣布

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

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

#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 

为什么第一个被保护VAR $file_log认识,第二个,$file_log_error,ISN是吗?

我已经尝试声明公共和私人的变量。同样的结果。

回答

2

$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' 
+0

我不能看到我的错误,太傻了!谢谢! – daniegarcia254