0

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
rstorbank
  • 11
  • 2
  • What does your `print_r($initDB->dbConn)` call show? – Koala Yeung Sep 09 '20 at 08:32
  • PHP Notice: Undefined variable: initDB in asfo\modules\smallInfo\smallInfo.class.php on line 12 PHP Notice: Trying to get property 'dbConn' of non-object in asfo\modules\smallInfo\smallInfo.class.php on line 12 – rstorbank Sep 09 '20 at 08:33
  • Just an aside - it may be worth reading https://www.php-fig.org/psr/psr-12/ for some coding standards. It's a good guide to some basic things which help code readability. – Nigel Ren Sep 09 '20 at 08:36
  • declare `$initDB` as a property first, then access it as `$this->initDB`, you already got it on the first class, just follow the same suit – Kevin Sep 09 '20 at 08:36
  • define a property `$dbConn` in class SmallInfo. use `$this->dbConn = $initDB->dbConnect();` in your constructor - now you can access your dbConn from other methods in your class using `$this->dbConn` – jibsteroos Sep 09 '20 at 08:37
  • for accessing variable property in inside your class you will have to declare it after class name. Then you will initialize it. After that you can easily access through $this keyword – Ariful Islam Sep 09 '20 at 08:44
  • Does this answer your question? [Understanding Constructor, $this keyword, and controller class in PHP/Laravel](https://stackoverflow.com/questions/38274735/understanding-constructor-this-keyword-and-controller-class-in-php-laravel) – yivi Sep 09 '20 at 09:25

3 Answers3

0

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 />";
  }
}
Koala Yeung
  • 7,475
  • 3
  • 30
  • 50
  • Wow... This is so dumb (for me)... I already tried the "print_r($this->initDB->dbConn);" solution but never once considered that $this-> was also required for the constructer... So obvious, but still missed it... Thanks. – rstorbank Sep 09 '20 at 08:55
  • For first beginners, it's easy to confuse attributes and local variables. Also different language has different implementation on the same concept. Your code manner (not the exact syntax) can probably run smoothly in Java. PHP requires you to be a little bit more explicit. – Koala Yeung Sep 09 '20 at 09:01
0

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 />";
    }
}
Polygnome
  • 7,639
  • 2
  • 37
  • 57
0

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 />";
}