Object-Oriented Connection Class in PHP

I started out in professional web development by using ASP.NET, so I was spoiled by the fact that Microsoft offers a SqlConnection class to use for various database related connectivity features. When I started with PHP, I decided to do some research and write a similar (albeit, much simpler) Connection class. Keep in mind that this class example only supports MySQL.

I started by defining some members of this class and came up with six useful members.

  • Database connection object returned from mysql_connect
  • Boolean to determine if the connection is currently open or closed
  • Database server host name
  • Database catalog name
  • Database user
  • Database password
class Connection
    public $dbConnection = null;
    public $isConnectionActive = false;
    private $dbServer = null;
    private $dbCatalog = null;
    private $dbUser = null;
    private $dbPassword = null;

In order to use this class, we need to add a constructor. I began by creating a default constructor that simply initializes values of the member properties. Take notice that not all of the properties are initialized by the default constructor. $dbConnection is not initialized until the Open() method is called and $isConnectionAlive is already false by default.

    public function Connection($server, $database, $username, $password)
        $this->dbServer = $server;
        $this->dbCatalog = $database;
        $this->dbUser = $username;
        $this->dbPassword = $password;

Now that the class can be instantiated, we need to add methods to allow operations. Below are the two methods that will perform the opening of the database connection and the closing of the database connection. Refer to the comments inline to understand what is happening.

    public function Open()
        if (!$this->isConnectionActive)    // if the connection is not alive, try to open
                // open the mysql connection with the member properties, select the catalog defined in the member property
                $this->dbConnection = mysql_connect ($this->dbServer, $this->dbUser, $this->dbPassword, true);
                mysql_select_db ($this->dbCatalog);

                if (!$this->dbConnection)    // if the connection failed, throw an error
                    throw new Exception('MySQL Connection Database Error: ' . mysql_error());
                    $this->isConnectionActive = true;    // the connection did not fail, the connection is now active
            catch (Exception $e)
                echo $e->getMessage();
        else    // the connection was already open
            return "Error: The connection has already been opened.";

    public function Close()
        if ($this->isConnectionActive)    // if the connection is open, try to close it
            $this->isConnectionActive = false;
        else    // if the connection is not open, it cannot be closed
            return "Error: No connection has been established to the database. Cannot close connection.";

That’s it! This is now a fully functional database connection class that allows for instantiation with opening and closing behaviors. See below for an example of its use. Note that the below example is not contained in the Connection.class.php file and is instead located in some other page that will be instantiating a Connection object.

$connection = new Connection($dbServer, $dbCatalog, $dbUser, $dbPassword);

// queries and other database operations here


The cool thing is that this class can be expanded to work with other databases.

Add a comment

Your email address will not be published. Required fields are marked *