2

I am building my first PHP OO project and am very much trying to avoid code duplication and use best practices for database and class design.

An issue I have is around my DAO and how they get / use the database connection.

This is what I have so far;

class DbConfig
{
    protected $db_host = 'hardcoded_host';
    protected $db_name = 'hardcoded_dbname';
    protected $db_user = 'hardcoded_user';
    protected $db_pass = 'hardcoded_pass';
}

As you can see - these values are hardcoded, I then extend this class with my DAO, meaning I would only need to alter these details in one place.

class Dao extends dbConfig 
{
    private $dbh;

    public function __construct()
    {
        // create $dbh PDO connection here and assign to private $dbh
        $this->dbh = $dbh;
    }

    public function createUser($username, $password)
    {
        // use $dbh connection and insert new user to db
    }

    public function checkUserCredentials($username, $password)
    {
        // used to check login details
    }

    public function createClient($name)
    {
        // as with createUser, except for client
    }
}

As you can see - I am performing CRUD operations on more than one database table / type - user and client.

I think the code would be better modulated and easy to understand if I had a user DAO and a client DAO (because there will be many more needed than these two) - but the problem I see is that I'd have to code the connection into each DAO.

Or I would have a connection class and pass this through the constructor of the DAO - is that a better option? The only issue I see is I would have to instantiate a connection class before I instantiate every DAO - and pass it into every method of all the DAO's.

eg:

$dbh = new databaseConnection();
$user = new UserDao();
$user->createUser($dbh);

This seems like unnecessary additional coding to me.

Should I stick to just one DAO? Or is there a way around having to write the connection method into every DAO if I create multiple? Using a trait perhaps?

EDIT: Another option - create te connection in my parent class DbConfig (top of the question), and then call parent::construct in each DAO's constructor thereafter. The problem I can forsee with this method is that I wouldn't be able to add any other initialisation code in the child classes.

Thanks

1 Answer 1

1

the way I'm solving this is having a class named DbConnection. I make this class a singelton class, so I have a method public static DbConnection getConnection() {...}. In your case this class can be inherited by DbConfig to get the db credentials.

There is another way to do this, according to your provided code here. So you wrote:

I think the code would be better modulated and easy to understand if I had a user DAO and a client DAO (because there will be many more needed than these two) - but the problem I see is that I'd have to code the connection into each DAO.

and

Should I stick to just one DAO? Or is there a way around having to write the connection method into every DAO if I create multiple? Using a trait perhaps?

you could write in this class Dao an protected void connect() method and create for other DAO-types classes which are inherited by Dao. So you have written your connection only once but could access it from all other DAOs.

So here an code example ;) It's based on your code!

class Dao extends DbConfig {

    /*
    * @type PDO
    */
    private $dbConnection;

    public function __construct() {
        // connect to db
    }
}

class UserDao extends Dao {
   // some other stuff explicit to this class

   public function __construct() {
       parent::__construct();
   }
}

Hope I got you right and this helped you!

Regards,

Clemens

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for this. Not sure I follow the 'another way' part though - could you provide some code please? Also - is all this unnecessary - ie should I just use one DAO, or use the parent::__construct method?
Ok, wrote an sample code. But as I said I recommend the first way an own singelton DbConnection-Class...
thanks a lot. why do you recommend the singleton method though?
I recommend a singelton CLASS.. sry if this was uncelar. Because you have an dedicated class only for your database connection. An in this class you can make all database operations, also sql injection safe.
sorry i know, but why do you recommend that way over the 2nd option I mean?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.