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.

Recommended Answers

All 6 Replies

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

$this->errorFile = ERROR_FILE;

Did you define $errorFile as a class variable ?

class MySql {
    private $errorFile;

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?

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.

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);
        }
    }

Thanks guys, I think you've helped me solve this!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.