0

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.

Edited by spowel4

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by spowel4
0

When referencing class instance variables, you need to do so as follows:

$this->errorFile = ERROR_FILE;
0

I did define $errorFile as a class variable, like this:

class Mysql {
    private $conn;
    private $dbServer;
    private $dbName;
    private $dbUser;
    private $dbPWD;
    private $errorFile;

    function __construct() {
        $dbServer = DB_SERVER;
        $dbName = DB_NAME;
        $dbUser = DB_USER;
        $dbPWD = DB_PASSWORD;
        $this->errorFile = ERROR_FILE;

What I don't understand yet, though, is that within the construct, if I apply the $this-> construction to the other class variables (conn, dbserver, etc...), then they quit working. Yet the errorFile variable only works if I use the $this-> construction. Any thoughts?

0

If you omit $this-> it is considered a local variable. It goes out of scope at the end of the constructor. Perhaps you are not referencing them correctly.

Edited by pritaeas

1

When handling class instance variables, you always need to use $this->. Therefore make sure you update every reference, as @pritaeas said. E.g.:

class Mysql {
    private $conn;
    private $dbServer;
    private $dbName;
    private $dbUser;
    private $dbPWD;
    private $errorFile;

    function __construct() {
        $this->dbServer = DB_SERVER;
        $this->dbName = DB_NAME;
        $this->dbUser = DB_USER;
        $this->dbPWD = DB_PASSWORD;
        $this->errorFile = ERROR_FILE;
        echo "Error file in mysql construct: " . $this->errorFile . '<br />';
        //$dsn = DSN;

        try {
            $this->conn = new mysqli($this->dbServer, $this->dbUser, $this->dbPWD, $this->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($this->errorFile, $e->getMessage(), FILE_APPEND);
        }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.