4

Is this a correct way to make PDO Connection.

I have different classes (each class have its own file) then there is config file contain PDO object and all classes objects. I am doing it correctly or there is any better practice.

classA.php

class classA {

    private $PDO;

    function __construct($PDO) { 
        $this->PDO = $PDO;
    }
    //other functions
}

classB.php

class classB {

    private $PDO;

    function __construct($PDO) { 
        $this->PDO = $PDO;
    }
    //other functions
}

classC.php

class classC {

    private $PDO;

    function __construct($PDO) { 
        $this->PDO = $PDO;
    }
    //other functions
}

And in config.php page:

include_once("db.php"); //contains db variables values
try
{
    $PDO = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
    die("Failed to connect database: " . $ex->getMessage());
}

require 'classA.php';
require 'classB.php';
require 'classC.php';

$objA = new classA($PDO);
$objB = new classB($PDO);
$objC = new classC($PDO);

include config.php on almost every page.

fmask
  • 481
  • 7
  • 18

2 Answers2

2

Short: yes, it's correct, but not perfect.

I think that you are doing it right. It's called Dependency Injection.

But your code has duplicates(DRY!). You could avoid it with using inheritance.

Also. Do not catch exceptions at all if you want just to "die".

include config.php on almost every page.

It would be better if you would do central point of entry. Front contoller pattern.

Community
  • 1
  • 1
sectus
  • 15,605
  • 5
  • 55
  • 97
  • thanks, can you please tell me how I can avoid `include_once("config.php")` for pages and have access to classes. Can you please tell me which code is duplicating and any hint to fix it. Thanks for helping me. – fmask Nov 01 '13 at 13:04
  • Your application should has one point of entry: index.php. [Read more](http://en.wikipedia.org/wiki/Front_Controller_pattern) – sectus Nov 01 '13 at 13:06
  • Thanks I'll study it further. – fmask Nov 01 '13 at 13:11
  • So PDO connecting method is perfect ? unless change the app structure to Front contoller pattern ? Or I can do it any better way. – fmask Nov 01 '13 at 13:16
  • @fmask, one class classA is perfect. But together they ruin DRY principle. – sectus Nov 01 '13 at 13:17
  • Also you could use lazy PDO realization. – sectus Nov 01 '13 at 13:19
  • Right now your code is not perfect. But i cannot say when it would be perfect: it's option based and it depends on your issues. – sectus Nov 01 '13 at 13:21
  • Thanks a lot for your help. so accepted answer here http://stackoverflow.com/questions/2047264/use-of-pdo-in-classes is good approach according to my situation? or it also come in to DRY! domain. – fmask Nov 01 '13 at 13:27
  • Actually no. There is one issue: [singleton](http://stackoverflow.com/questions/4595964/who-needs-singletons) – sectus Nov 01 '13 at 13:35
  • Great so Dependency Injection is better than Singleton and I can use current connection method while I move app to Front controller pattern. – fmask Nov 01 '13 at 13:57
0

According to your situation your approach is fine. ( Not Front controller pattern)

Avoid singleton as Dependency Injection is clean and maintainable.

Even Erich Gamma, one of the Singleton pattern's inventors, doubts this pattern nowadays:

"I'm in favor of dropping Singleton. Its use is almost always a design smell"

Domnic
  • 43
  • 1
  • 6