2

I've build up new DB connection class for simple MVC pattern.

I need to know this is the correct way to do this.

<?php

include_once 'config.php';

class dbModel{
    private $dbSys      = "";
    private $dbHost     = "";
    private $dbUser     = "";
    private $dbPass     = "";
    private $dbName     = "";
    private con = false;

    public function __construct(){
        $this->dbSys    = DB_SYS;
        $this->dbHost   = DB_HOST;
        $this->dbUser   = DB_USER;
        $this->dbPass   = DB_PASS;
        $this->dbName   = DB_NAME;

        if (!$this->con){
            try{
                $this->con = new PDO($this->dbSys.':host='.$this->dbHost.';dbname='.$this->dbName, $this->dbUser, $this->dbPass);
                return $this->con;
            } catch (PDOException $e){
                echo $e->getMessage();
                exit();
            }
        }else{
            return $this->con;
        }
    }
}
?>

I included config.php file for database configuration as a separate file. I'm creating new object from this db connection class in other Model in my project and writing sql and run queries.

I tested this code and this is working.but I need to know this right way or not.

Please let me know this is correct way.

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
cgw9151
  • 21
  • 1
  • 2
  • 6
  • They'll send you to codereview site. Ironically, you hardly get a relevant answer there. – Your Common Sense Oct 02 '13 at 14:48
  • 1
    I think you should make your class a singleton !! – Anas Oct 02 '13 at 14:49
  • 2
    @Anas [**Don't**](http://stackoverflow.com/a/4596323/1607098) – Touki Oct 02 '13 at 14:50
  • 3
    This has NOTHING to do with MVC. – tereško Oct 02 '13 at 14:50
  • Yeah, this belong on Code Review. – Martin Bean Oct 02 '13 at 14:52
  • 1
    [This topic](http://stackoverflow.com/questions/11369360/how-to-properly-set-up-a-pdo-connection) might help on the PDO-side of things. – tereško Oct 02 '13 at 14:52
  • 3
    You should use dependency injection – ILikeTacos Oct 02 '13 at 14:53
  • Why are you writing your own MVC framework when there are [many out there that are already feature complete](http://codegeekz.com/best-php-frameworks-for-developers/)? – tadman Oct 02 '13 at 14:59
  • @tadman sometimes it's a need – Royal Bg Oct 02 '13 at 15:01
  • Because everyone have to learn. If noone written their own, we wouild had no frameworks at present :) – Your Common Sense Oct 02 '13 at 15:02
  • @YourCommonSense If this is an academic exercise, sure, but 99% of the time, someone writing their own MVC is either going to make the same mistakes as everyone before them, or they're going to spend an extraordinary amount of time to make something that barely meets their own needs. It's usually better to find a framework that's close enough, then extend it as you require. – tadman Oct 02 '13 at 15:12
  • @tadman - you can't know what you need until you try and see what you've done is wrong. Frameworks and patters are great for people with experience in doing the same task over and over without tools at hand, forced to create them on the fly. It's easy to advise what's good, just bear in mind that you probably created tons of code that at this point exists in a better format. There's nothing wrong with reinventing the wheel, you learn certain things for good that way. – N.B. Oct 02 '13 at 15:19
  • Unfortunately, in most cases people simply adhere to "My Very Code" pattern. – tereško Oct 02 '13 at 15:19
  • @tadman, most of the MVC's implements the logic you are just making a CMS or a simple Web Site with them. In my workplace, for the last project, which is an Online MMO game, none of the frameworks was fittable for the needs, starting with that, the software behind is not just simple Apache, reaching the problem that templates have to be controlled not from the controller, but from another abstraction, so our team had to write own framework from scratch – Royal Bg Oct 02 '13 at 15:23
  • @RoyalBg There are frameworks that are geared towards a CMS, and others that are much more open-ended. To start with an empty text file and create your site, without even trying to use a framework, is almost always the wrong way to go about it. Nobody in the Python or Ruby world would do this, why do so many PHP people think they're somehow exceptional, that they can do this without getting into trouble? – tadman Oct 02 '13 at 16:00
  • Because this is PHP :) A script language. Where you are not bound to limits. And the most used web language. I, personally, consider using Ruby or .NET for web app's a waste of resources, where you are also bound to limits – Royal Bg Oct 02 '13 at 19:36

1 Answers1

7

Well, I see absolutely no point in this class at all. It is too leaky abstraction to be used this way, yet it adds no benefit over regular PDO class.

There is also an awful lot if useless code: for example, you define your credentials THREE times, to use them only once.

Error reporting is also useless and wrong.

I'd make this whole file of four lines only

<?php
include_once 'config.php'
$pdo = new PDO(DB_SYS.':host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

yet it will be more flexible and error proof than yours.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345