Reputation: 25
I'm just getting started with OOP PHP and all of the information out there is about a car that is red or blue...it's hard to get my head around the database connection only object as such.
I have a haunting suspicion that my __construct() shouldn't have the connection string in it and instead be it's own method inside the class.. but it works so....
Is it incorrect to define my connection class like below.. If it is wrong - how should it look?
class dbConnect {
// Declare connection info for PDO
private $dbType = 'mysql';
private $dbHost = 'localhost';
private $dbUser = 'user';
private $dbPass = 'password';
private $dbName = 'db';
// Declare connection variable for object
private $dbConn;
// Construct object
private function __construct() {
// Create Database connection and assign it to object as dbConn
$this -> dbConn = new PDO( $this -> dbType . ':' . 'host=' . $this -> dbHost . ';' . 'dbname=' . $this -> dbName , $this -> dbUser , $this -> dbPass );
}
}
Upvotes: 1
Views: 5209
Reputation: 72652
I don't think what I think you are doing is very useful. It looks like you are going to have a database class. I just cannot see the advantage of doing it like that because PDO
already is a class, so there is no need to extend it unless you have a very good reason to do so.
A much better option imho would be the initialize the PDO
instance at bootstrap phase and inject the connection in the classes that need it;
$dbConnection = new PDO('mysql:host=localhost;dbname=db', 'user', 'password');
$someInstance = new ClassThatNeedsDatabase($dbConnection);
$someInstance->doSomething();
class ClassThatNeedsDatabase
{
private $dbConnection ;
public function __construct(PDO $dbConnection)
{
$this->dbCOnnection = $dbConnection;
}
public function doSomething()
{
$stmt = $this->dbConnection->prepare('UPDATE....');
// etc
}
}
On a generic note you may want to avoid using the new
keyword inside other classes, because they introduce tight coupling. Which will hurt maintainability (it's hard to tell the class is used by looking at the class signature), testablity (you cannot easily swap the class) etc. If you really need to build new instances inside other classes (which in this specific case is really not needed imo as stated above) you may want to implement the factory pattern:
class Foo
{
public function doSomething()
{
}
}
class FooFactory
{
public function build()
{
return new Foo();
}
}
class Bar
{
private $fooFactory;
public function __construct(FooFactory $fooFactory)
{
$this->fooFactory = $fooFactory;
}
public function soSomethingWhichNeedsToBuildAnInstance()
{
$foo = $this->fooFactory->build();
}
}
This also prevent tight coupling of code.
The above naming sucks, but it is just to illustrate the point. :-)
Upvotes: 6
Reputation: 6687
Yes, you should be extending
the existing PDO
object.
How about this as your base:
define('DB_USER', 'username');
define('DB_PASS', 'password');
define('DB_DSN', 'dsn');
class dbConnect extends PDO {
public function __construct($user = DB_USER, $pass = DB_PASS, $dsn = DB_DSN) {
parent::__construct($dsn, $user, $pass, $options);
}
}
Upvotes: 0