0

New to OOP and trying to understand some basic fundamentals. Currently using PHP5 to build a basic web app.

In using PHP's built in PDO as my database class and a separately created user class that handles, among other things, user log in and out, is it best to have the db class connect to the database in a log in situation, or does the user class need to directly connect to the db? If the latter, I think I'm likely misunderstanding how PDO would standalone.

If the former, presumably some kind of 'helper class' needs to be created that would facilitate the db connection between PDO and my user class?

2
  • Do you feel there is something special about a user class or just domain classes in general?
    – JeffO
    Commented Nov 21, 2013 at 16:44
  • I think my struggle is more fundamental than that. I think I'm misunderstanding the relationship between classes and how they interact with each other. For example, if the user class handles log in but the db class handles the actual connection, how do those two interact so that the connection is made and the user can log in? Is that done by way of the logic code, or within the objects themselves?
    – Ryan
    Commented Nov 22, 2013 at 9:12

1 Answer 1

0

Basically you want only one connection to the db. The responsibility for connecting to and "talking" with the db should not be shared among many classes. You'll have to copy-paste host, username, password information, which will be quite bad, when you move your site around, and you have to hunt down all instances of connecting to the database and rewrite it. Pretty baaad.

So, my advice: login related functionality goes in Login class, DB related in DataBaseHandler class, formatting HTML in its own class, etc.

Example for DataBaseHandler:

DataBaseHandler.php:

class DataBaseHandler
{
     private $mysql_host = "myhost.example.com";
     ....

     function Connect()
     {
          $this->mysqli_object = mysqli_connect($this->mysql_host, $this->mysql_username, $this->mysql_password, $this->mysql_db);

          if ($this->mysqli_object->connect_errno)
          { 
               print "Failed to connect to MySQL: " . $this->mysqli_object->connect_error;
          }
     }

     ....
}

$dbHandler = new DataBaseHandler;
$dbHandler->Connect();

If you end up writing mySqlObject->connect(...) -s everywhere, you will be making a lot of work for yourself.

Update (regarding the comment) The way I used it (probably not the best practice, since I had stopped writing php years ago) was I included the file of the class everywhere I needed. But you can ask around what's the best practice for that (static? global? composition???) in php. (In the simplest case in C# I would go for a lazy evaluated singleton, probably :))

1
  • Great, that I understand. So if a user of my site then wants to log in (and the log in functionality is in another class, i.e. Log in/User etc., once I instantiate the db connection object, how does the user object interact with it to use that connection?
    – Ryan
    Commented Nov 22, 2013 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.