Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to create a DAO in PHP

Tags:

php

mysql

dao

From an OOP perspective, what is the proper way to create a DAO (Data Access Object) in php?

For example (using Account as a basic example), my AccountDAO will have some of the following functions:

  • GetAllAccounts
  • GetAccountByID
  • UpdateAccount
  • DeleteAccount
  • InsertAccount

So once I fetch all of the Accounts in the database, should I return them to the caller as an array of account objects? Should I just return the mysql result set?

Do you know any good example of a DAO?

like image 673
carlg Avatar asked Nov 24 '22 21:11

carlg


1 Answers

You are on the right track to build a DAO: the methods you listed should definitely be part of a DAO.

To answer your specific question, a DAO should not leak any database specific objects, it can leak some database concepts though as long as they are not specific to a database vendor.

So no, you should not return a MySQL result set. But you could create a generic result set that wraps a MySQL result set and that may work with higher level concepts (extracting whole objects instead of extracting columns).

Edit: My intent is not to lead you to actually build a result set wrapper, you could also simply return an array of objects like you initially suggested. This was just an example.

like image 152
Vincent Robert Avatar answered Dec 10 '22 03:12

Vincent Robert