I have a class with a required file:
require_once 'includes/constants.php';
That required file contains the following:
<?php
// Define constants here
define("DB_SERVER", "localhost");
define('DB_USER', 'xxxxxxxxxx');
define('DB_PASSWORD', 'xxxxxxxxxx');
define("DB_NAME", "customerInfo");
define("ERROR_FILE", "c:\wamp\www\customerinfo\Errors.txt");
The class has a constructor:
function __construct() {
$dbServer = DB_SERVER;
$dbName = DB_NAME;
$dbUser = DB_USER;
$dbPWD = DB_PASSWORD;
$errorFile = ERROR_FILE;
echo "Error file in mysql construct: " . $errorFile . '<br />';
//$dsn = DSN;
try {
$this->conn = new mysqli("$dbServer", "$dbUser", "$dbPWD", "$dbName") or die();
//$this->conn = new PDO($dsn);
//$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
}
catch (Exception $e) {
echo "There has been a problem connecting to the database.\nPlease refer to $errorFile for the cause of the problem.\n\n";
file_put_contents($errorFile, $e->getMessage(), FILE_APPEND);
}
One of the functions/methods in the class is this (I put it in for debugging):
function ShowErrorFile() {
echo "Error File in ShowErrorFile function: " . $this->errorFile . '<br />';
}
When I call that method from another php page, like this:
<?php
$mysql = New Mysql();
$mysql->showErrorFile();
?>
The error file comes up empty/null and I don't understand why. The only way I can get the methods within the class to see the error file is by including this at the top of the class:
private $errorFile = ERROR_FILE;
Can someone explain to my dumb brain what's going on? I thought that by including the error file definition in the constructor, the methods would know what the error file is.