php - Not quite understanding how to use a constructor it seems

I am making a minor CMS for specific use at my current job, and I want to do it right... hence OOP.

For this I want to make a database connection class that I can use in my other classes when I need to make database queries. However, for some reason, I keep being stopped at the same point, because I don't seem to be able to get the connection string into my classes.

Whatever changes I make, I end up with "undefined... " something every time.

File - databaseManagement.class.php:

class database {
private $user;
private $pass;
private $db;
private $serv;
private $type;
private $dsn;
private $sqlsrvString;
private $charset;
private $dbIni;
private $options;
public $connectionInfo;
public $dbConn;

public function __construct() {
    $this->dbIni = parse_ini_file('settings/database.ini.php');

// ... assign values to individual variables based on values in the database.ini.php file.
    $this->user = $this->dbIni['user'];
    $this->pass = $this->dbIni['pass'];
    $this->db = $this->dbIni['db'];
    $this->serv = $this->dbIni['serv'];
    $this->type = $this->dbIni['type'];
    $this->charset = $this->dbIni['charset'];
    
    $this->connectionInfo = array( 
        "Database"=>$this->db, 
        "UID"=>$this->user, 
        "PWD"=>$this->pass
    );

}

public function dbConnect() {
    $this->dbConn = sqlsrv_connect($this->serv, $this->connectionInfo);
}

File - smallInfo.class.php

class smallInfo {
function __construct() {
    $initDB = new database();
    $initDB->dbConnect();
    
    var_dump($initDB);
}

function showDkProgress(){
    echo "<hr />";
    print_r($initDB->dbConn);
    echo "<hr />";
}

Now the var_dump() in smallInfo constructor returns all the expected values, including those of $dbConn, however, I can't seem to access $dbConn in showDkProgress() for use in queries.

Answer

Solution:

Two problems:

  1. Constructors does not set variables (e.g. $variableName) as object attributes. You have to explicitly assign it with $this->variableName assignment.

  2. Unlike Java, the OOP implementation in PHP does not automatically alias attributes. The variable $variableName in a method does not automatically equal to $this->variableName.

So:

class smallInfo {
  function __construct() {
    $this->initDB = new database();
    $this->initDB->dbConnect();
    var_dump($this->initDB);
  }

  function showDkProgress(){
    echo "<hr />";
    print_r($this->initDB->dbConn);
    echo "<hr />";
  }
}

Answer

Solution:

In your smallinfo class, initDB is a local variable inside the constructor. It doesn't exists outside the constructor.

Thus, accessing an undefined variable named initDB in showDkProgress yields an error.

You need to make initDB a field.

class smallInfo {

    private $initDB;

    function __construct() {
        $this->initDB = new database();
        $this->initDB->dbConnect();
    
        var_dump($this->initDB);
    }

    function showDkProgress(){
        echo "<hr />";
        print_r($this->initDB->dbConn);
        echo "<hr />";
    }
}

Answer

Solution:

Function showDkProgress() is unable to get it since it is only local variable in constructor. If you embed $initDB in class you should add field for it.

so try eg:

class smallInfo {

  public $initDB;

  function __construct() {
    $this->initDB = new database();
    $this->initDB->dbConnect();

  function showDkProgress(){
    $si = new smallInfo();
    echo "<hr />";
    print_r($this->initDB->dbConn);
    echo "<hr />";
}

Source