One of the unique features of PHP is the ability to use implemented constants from interfaces. In most other languages, an interface is wholly abstract. You can only add abstract methods to an interface—no properties can be placed in an interface, abstract or concrete. This little post shows how to use constants in an interface, along with an abstract method, to create a re-usable connection application. In using this little application, I’ve found it to be easy to re-use, and while it is not a design pattern, it is so flexible and loosely coupled that it reflects the spirit of design patterns and OOP in PHP.
The Interface
Like all interfaces, you want to have a general structure outlined. In this interface, I’ve included all of the connection information needed to use in conjunction with PHP’s Mysqli class. Note, this uses Mysqli—with an “i” for ‘improved’.
//Filename: IConnectInfo.php
interface IConnectInfo
{
const HOST ="localhost";
const UNAME ="phpDP";
const PW ="looseCoupling";
const DBNAME = "dpExpress";
public function doConnect();
}
?>
|
There’s also the abstract method, doConnect(), that is written as all abstract methods. It has no parameters and is to be accessesed publicly. (It has public visibility.) Each of the constants are assigned a concrete value that is passed to the class that implements it.
A Universal Connection Class
Next, we can create a universal connection class containing the single method defined in the interface. We can also use the constants’ values to make the connection. To do this requires using the scope resolution operator (::) because there are no instances of the constants. We cannot instantiate an instance of an interface, but we can get the values of the constant using the scope resolution operator as shown in the following implementation:
//ini_set("display_errors","1");
//ERROR_REPORTING(E_ALL);
include_once('IConnectInfo.php');
class UniversalConnect implements IConnectInfo
{
private $server=IConnectInfo::HOST;
private $currentDB= IConnectInfo::DBNAME;
private $user= IConnectInfo::UNAME;
private $pass= IConnectInfo::PW;
public function doConnect()
{
$hookup=new mysqli($this->server, $this->user, $this->pass, $this->currentDB);
if (mysqli_connect_error())
{
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
return $hookup;
}
}
?>
|
Each of the connection constant’s values are encapsulated in private variables and then used in the implemented method, doConnect(). Importantly, the class has no constructor function; so it doesn’t fire off as soon as it’s instantiated.
Doing Connections
In Figure 1, you can see the relationship between the different objects that make up the connection structure. In some respects the UniversalConnect class is like an abstract class in that nothing happens when it is instantiated. (Of course you cannot instantiate an abstract class). It is used by concrete classes to make a connection by calling the doConnect() method in the UniversalConnect object.
So how does this make life easier and more productive for the programmer? Instead of putting in the entire connection routine, along with all of the correct connection names, it only takes two simple lines to make a connection:
$operation=new UniversalConnect();
$this->hookup=$operation->doConnect();
The private variable $this->hookup is the name of the connection variable that uses the doConnect() method from the UniversalConnect class. To get a better idea of how it works, the following class, Update, is a simple PHP class for updating data in a table.
include_once('../UniversalConnect.php');
class Update
{
private $hookup;
private $sql;
private $tableMaster="productTable";
private $id;
private $field;
private $fValue;
public function __construct()
{
//Two lines for entire connection operation
$operation=new UniversalConnect();
$this->hookup=$operation->doConnect();
//
$this->id=$_POST['recordID'];
$this->field=$_POST['fieldNow'];
$this->fValue=$_POST['changeNow'];
$this->sql ="UPDATE $this->tableMaster SET $this->field='$this->fValue' WHERE id='$this->id'";
if ($result = $this->hookup->query($this->sql))
{
echo "Record $this->id in field [$this->field] has been changed to: $this->fValue";
}
$this->hookup->close();
}
}
$doUpdate=new Update();
?>
|
Given the loose coupling between the implementation and the connection values, you can easily change the connection values without having to re-write the connection information for a whole set of classes that use a different set of values. For example, if you have two customers who want roughly the same back end, instead of revising all of the classes where a different set of connection values are used, you can just change the connection values in the single interface without having to change anything else.
Naturally, I’d be very interested in any feedback, questions or comments you might have about using this kind of loose coupling with PHP-MySQL connections.
Recent Comments