-1

Okay so I have some what of a dumb question, I've seen tuts and different things on making a CMS and I want to make an oop CMS, and I was wondering if someone could explain to me what the difference was between using one of the two examples?

Ex 1 -

 class myClass
 {
      var $username;
      var $password;

      public function connect()
      {
           try {
                $pdo = new PDO('mysql:host=localhost;dbname=dbname', $this->username,
                     $this->password);
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                return $pdo;
           } catch(PDOException $e) {
                echo 'Error: ' . $e->getMessage();
           }
       }
 }
 // Then to call that function
 $obj = new myClass();
 $obj->username = "root";
 $obj->password = "password";
 $pdo = $obj->connect();
 // Then run my query down here

Ex 2 -

 class database
 {
      protected $connection = null;
      //make a connection
      public function __construct($hostname,$dbname,$username,$password)
      {
           try {
                //MySQL with PDO_MYSQL  
                $this->connection = new PDO("mysql:host=$hostname;dbname=$dbname",
                     $username, $password);
                $this->connection->setAttribute(PDO::ATTR_ERRMODE,
                     PDO::ERRMODE_EXCEPTION); 
           } catch (PDOException $e) {
                $this->connection = null;
                die($e->getMessage());
           }
       }
  }

Or I've even seen people use a __construct then a seperate connect function So what exactly is the difference? Is there a performance benefit by doing it a particular way? Or is there a way that is more correct than the other or are all three if these incorrect ways of doing it? I haven't found a reliable source to find an answer.

user2701687
  • 327
  • 1
  • 5
  • 13
  • I'm not usre performance wise, but I'd do it the second way, so the class has it's own connection, and you don't need to manage it externally like in example one. – TMH Nov 27 '13 at 17:53

2 Answers2

2

For the most cases 3rd example is the best:

In fact, PDO is already a database class. So, if you have no particular reason to create another on top of it, PDO itelf is just a perfect:

$pdo = new PDO('mysql:host=localhost;dbname=dbname', $username,$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

is all you actually need.

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

There are no noticeable performance differences between the two.

The second way is preferred by a lot of people because a database object would be kind of useless if it didn't try to connect to a database. Because attempting to connect to a database is so vital to the object's worth / existence, it makes sense to send in the connection details via the constructor so that it can attempt a connection as soon as it is being instantiated. Thus, changing:

$obj = new myClass();
$obj->username = "root";
$obj->password = "password";
$pdo = $obj->connect();

to

$obj = new myClass('localhost', 'root', 'mypassword');
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • Okay so if I go on using the second example, that will make that entire class have its own connection correct? And every time that that class is run it will re connect to the db? Ultimately this connect function will be part of a CMS class that I am planning on building, so would this still be the best option? – user2701687 Nov 27 '13 at 19:45