Creating a Database Connection As Far As We Obey to OOP
Tags: php, beginners, webdev, oop
Published: 2018-08-09 11:27:48

---
title: Creating a Database Connection As Far As We Obey to OOP
published: true
description: We will learn interfaces and constants in PHP.
tags: #php #beginners #webdev #oop
---

In this post, we will learn a pretty database connection in PHP. We have to obey the OOP with this case. Firstly, i will have interface file in project directory called `IconnectionInfo.php`. As below. But we cannot include variables in interfaces, you can include constants.For example, the following interface has constants used in a MySQL connection:

```php
<?php

interface IConnectInfo
{
    const HOST ="localhost";
    const UNAME ="root";
    const DBNAME = "symf0ny";
    const PW = "sem";
    function getConnection();
}
?>
```

Implement the interface just like any other PHP interface. The values of the constants can then be passed using the scope resolution operator in the implementation:

```php
<?php

include_once('IConnectInfo.php');

class Database implements IConnectInfo
{

    private static $instance = null;
    private $conn;

    private $server = IConnectInfo::HOST;
    private $currentDB = IConnectInfo::DBNAME;
    private $user = IConnectInfo::UNAME;
    private $pass = IConnectInfo::PW;

    private function __construct()
    {
        try {
            $this->conn = new PDO("mysql:host=$this->server;dbname=$this->currentDB", $this->user, $this->pass
            );
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conn->exec('set names utf8');
            echo $this->server . " connected successfully" . PHP_EOL;
        } catch (PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
            die;
        }
    }

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new Database();
        }

        return self::$instance;
    }

    public function getConnection()
    {
        return $this->conn;
    }


    public function getSelectQueryResult($query = '')
    {
        try {
            $query = $this->conn->prepare($query);
            $query->execute();
            return $query->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            echo $query . "<br>" . $e->getMessage();
        }
    }
}
```

The only method is getConnection(), but your interface could be made up of nothing but constants if you wanted. The values are passed to the class properties (private variables in the example) using the name of the interface (IConnectInfo), the scope resolution operator, and the name of the constant.

Well, we need only initializer file. It is named `app.php`. The following codes means it.

```php
<?php

require __DIR__ . '/class/Database/Database.php';

$dbInstance = Database::getInstance();
$dbConnection = $dbInstance->getConnection();
```

From now on, we can get database connection.

![alt text](https://thepracticaldev.s3.amazonaws.com/i/g9j7yquzlorxdq0bh25c.png "Pretty Database Connection")

That's cool! My goal is precisely enlighten our minds with OOP and database connection. I hope, i could do it. In this article, so much for now. We have learned connect any database connection with interfaces and constants.