0

Trying to understand and use OOP in PHP I have a class called dbcon. I am following a tutorial in youtube which is using the protected connect() function to connect to db. Now my question is why not connect to db in the constructor?

function __construct() {
      $this->DBSERVER  = "localhost" 
      $this->DBUSERNAME = "root" 
      $this->DBPASSWORD = "" 
      $this->DBNAME    = "thedb" 

      $conn = new mysqli($this->DBSERVER, $this->DBUSERNAME, $this->DBPASSWORD, $this->DBNAME);
      if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
      }     
      return $conn;
    } 
    }

can someone let me know what is the benefit or downside of this?

<?PHP
class dbcon {
    private $DBSERVER;
    private $DBUSERNAME;
    private $DBPASSWORD;
    private $DBNAME;

    protected function connect(){
      $this->DBSERVER   = "localhost" 
      $this->DBUSERNAME = "root" 
      $this->DBPASSWORD = "" 
      $this->DBNAME     = "thedb" 

      $conn = new mysqli($this->DBSERVER, $this->DBUSERNAME, $this->DBPASSWORD, $this->DBNAME);
      if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
      }     
      return $conn;
    }
}
?>
Mirko Steiner
  • 354
  • 1
  • 5
Mona Coder
  • 6,212
  • 18
  • 66
  • 128
  • 1
    in short, why not? It is a design decision, your decision to be precise. The up/downside(s) of the way you design your dbconn() class will depend on how it (needs to) communicate(s) with other objects in your application. One recommendation, if I may, familiarize yourself with coding to an [interface not an implementation](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface), it will make your code more flexible and future proof. – jibsteroos Feb 05 '19 at 19:47
  • You cannot return a value from a constructor, you would normally set an instance variable. – Nigel Ren Feb 05 '19 at 20:09
  • I think [this](https://stackoverflow.com/a/3032822/2734189) is a good general explanation of constructor usage, and [this](https://stackoverflow.com/a/43631201/2734189) is more how I would recommend handling the connection. – Don't Panic Feb 05 '19 at 20:24

1 Answers1

0

My first thought about this is, that this could be hard to unit-test. Everytime you make in instance from that class, there MUST be a database where you can connect to, else it would fail.

Of course, the connect() method is still hard to test, but you could test the rest easily.

There is a trick to call a method and create an instance of a class in one line:

($myDBInstance = new dbcon("localhost", "mirko", "mysecret", "mydb"))->connect();

Another way would be to call a static method, which makes it clear, that is automatically connects, if you dont like the above solution:

$myDBInstance = dbcon::getInstanceAndConnect("localhost", "mirko", "mysecret", "mydb");

which could look like:

public static getInstanceAndConnect(a,b,c,d) {
     $mydbcon=new dbcon(a,b,c,d);
     $mydbcon->connect();
     return $mydbcon;
}

sorry for lazy arguments :-)

Mirko Steiner
  • 354
  • 1
  • 5