duanpao6163 2016-01-30 18:51
浏览 32
已采纳

访问后未定义的变量/空属性错误

This is driving me crazy and I hope someone can help.

I am learning PHP and as part of a larger project to help me debug/log issues as they happen I have created a PHP error logging class.

This is my error:

This is a test

I hope it worked!

Success!

Notice: Undefined variable: errorLog in /Applications/MAMP/htdocs/errorlog.php on line 27

Fatal error: Cannot access empty property in /Applications/MAMP/htdocs/errorlog.php on line 27

So I can see

The error on line 27 consists of:

$outputStringForSQL = $conn->escape_string($this->$errorLog);

I am stumped. I don't understand how $errorLog is undefined as it has clearly been added to twice with the append() method, and read from using the returnLog() method. The property $errorLog is clearly not empty as returnLog() reads from it fine. I have checked that $this->errorLog is not empty by echoing it out.

I thought maybe I misunderstood scope so set $erroLog to public, still nothing.

Here is errorlog.php the class 'errorlog':

<?php
class errorlog{

protected $errorLog;

public function append($string){
  $this->errorLog = $this->errorLog . $string . "<br />";
}

public function returnLog() {
  echo $this->errorLog;
  return "Success!";
}

public function commit(){
  global $_mysqlUsername;
  global $_mysqlPassword;

  $servername = "localhost";
  $dbname = "my_DB";    //production

    // Create connection
  $conn = new mysqli($servername, 'root', 'root', $dbname);
    // Check connection
  if ($conn->connect_error) {
       die(error_log($conn->connect_error));
   }
   $outputStringForSQL = $conn->escape_string($this->$errorLog);
   $sql = "INSERT INTO `tblDebugLog` (`ID`, `dateTime`, `debugData`) VALUES (NULL, CURRENT_TIMESTAMP, '$outputStringForSQL');";
   if ($conn->query($sql) !== TRUE) {
     echo("Error: " . $sql . "<br>" . $conn->error);
   }
   $conn->close();
   return 'Success';
   }
}
?>

This is the script making use of the 'errorlog' class:

<?php
   function __autoload($class){
      require "$class.php";
   }

   $myError = new errorlog;
   $myError->append('This is a test');
   $myError->append('I hope it worked!');
   echo $myError->returnLog();
   echo $myError->commit();
 ?>

Can anyone shed some light on this bug?

Many thanks.

  • 写回答

1条回答 默认 最新

  • doufu7464 2016-01-30 18:52
    关注

    Replace $this->$errorLog to $this->errorLog

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?