PHP类给出声明的未定义公共变量的错误
我正在为记录器编写一个PHP类。我已经声明了两个公共变量PHP类给出声明的未定义公共变量的错误,php,class,object,logging,variable-declaration,Php,Class,Object,Logging,Variable Declaration,我正在为记录器编写一个PHP类。我已经声明了两个公共变量$file\u log和$file\u log\u error class Logger { //constants declaration const FILE_BASE = '/log/comunio-uk-log-'; // property declaration protected $file_log = ''; protected $file_log_error = ''; /
$file\u log
和$file\u log\u 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>";
}
}
}
为什么第一个受保护的var$file\u log
被识别,而第二个受保护的var$file\u log\u error
,不是吗?
我已经试着将VAR声明为公共和私有。相同的结果。
$file\u log\u error
从未定义。替换
与
如果你写信
$file\u log\u错误
从未定义。替换
与
如果你写信
#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
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'