Reputation: 222
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.
Upvotes: 1
Views: 305