HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Specific PHP Data Access Class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classphpspecificdataaccess

Problem

As part of a fun project to help build my knowledge of PHP. I've written a Data Access Class here to bridge the site to the database. I know there are a lot of posts and articles out there explaining this type of thing, but I needed some individual feedback from people.

It does work, but I'm open to all sorts of responses! I'm torn between which format to work with, but I've been following Zend's code formatting documents. If you're familiar with that, I'd love to hear where my code is wrong there.

Any speed issues I'd gladly hear. I'm also open to code recycling tips.

```
class DataBase
{
/**
* @var null|\PDO
*/
private $_connection = NULL;
/**
* @var string
*/
private $_databaseHost = 'localhost';
/**
* @var string
*/
private $_databaseUser = 'root';
/**
* @var string
*/
private $_databasePassword = '';
/**
* @var string
*/
private $_returnMethod = 0;

const DECIDE_RETURN_METHOD = 0;
const ASSOC_ARRAY_RETURN_METHOD = 1;
const LAST_ID_RETURN_METHOD = 2;
const BOOLEAN_RETURN_METHOD = 3;
/**
* Construct a Database object. Use this to query the database(s).
*
* @param string $database The database to start a MySQL connection with.
* @param string $host The host the connection will be with.
* @param string $username The username the connection will be with.
* @param string $password The password the connection will be with.
*
* @throws Exception If the database could not be connected to.
*/
public function __construct($database, $host = 'localhost', $username = 'root', $password = '')
{
$this->_databaseHost = $host;
$this->_databaseUser = $username;
$this->_databasePassword = $password;
try {
$dsn = 'mysql:dbname=' .$database. ';host=' .$this->_databaseHost;
$this->_connection = new PDO($dsn, $this->_databaseUser, $t

Solution

Just a few random notes:

-
Correct me if I'm wrong but if you throw an exception the statements after the throw statements won't be run. So here return FALSE never runs, therefore it's unnecessary:

throw new Exception('Database connection not available.');
return FALSE;


-
The query_db function could use guard clauses to make the code flatten:

private function query_db($sql, array $data) {
    if (!is_string($sql) || !isset($sql[9])) {
        throw new Exception('Invalid SQL string.');
    }
    if (is_null($this->_connection)) {
        throw new Exception('Database connection not available.');
    }
    try {
    ...
}


References:

  • Replace Nested Conditional with Guard Clauses in Refactoring: Improving the Design of Existing Code;



  • Flattening Arrow Code

Code Snippets

throw new Exception('Database connection not available.');
return FALSE;
private function query_db($sql, array $data) {
    if (!is_string($sql) || !isset($sql[9])) {
        throw new Exception('Invalid SQL string.');
    }
    if (is_null($this->_connection)) {
        throw new Exception('Database connection not available.');
    }
    try {
    ...
}

Context

StackExchange Code Review Q#40061, answer score: 5

Revisions (0)

No revisions yet.