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

Learning OOP PHP, simple MySQL connection class.

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

Problem

I have posted an earlier version of this and here is the improved version from the feedback I recieved. Some of the feedback I received was;

  • Don't chain method (tried my best to limit this)



  • Do not use print() or die() and an error response (still a little lost, but I did attempt to use a redirect to a custom error page)



I tried my best to rewrite the code, I am still very new to this so go easy. I did read up on OOP methodology and read about Interfacing and Implementation, so I tried to incorporate that in my class.

I would love to see some different ideas on how to make this the most efficient as possible. I am sure it needs a ton of changing, but that is why I am here to help me learn and grow. I am a visual learner so if possible actual code would be awesome, but any response would be greatly appreciated.

```
user = $user;
$this->pass = $pass;
$this->data = $data;
$this->host = $host;
$this->process();
}

/ INTERFACE /
private function process()
{
if($this->verifyNullFields()==true)
{
if($this->verifyDatabaseConnection()==true)
{
if($this->verifyDatabaseExist()==true)
{
print('ALL PASSED'); //for debugging
}
else
{
print('redirect to custom error page will go here');
}
}

else
{
print('redirect to custom error page will go here');
}

}
else
{
print('redirect to custom error page will go here');
}

Solution

Let me show my version of this code:

DatabaseException.php:

class DatabaseException extends Exception {
}


Database.php:

abstract class Database {
    protected $login;
    protected $password;
    protected $database;
    protected $hostname;

    public function __construct($login, $password, $database, $hostname) {
        // NB: password not checked and may be empty
        $this->throwExceptionIfNotSet('login', $login);
        $this->throwExceptionIfNotSet('database', $database);
        $this->throwExceptionIfNotSet('hostname', $hostname);

        $this->login    = $login;
        $this->password = $password;
        $this->database = $database;
        $this->hostname = $hostname;
    }

    private function throwExceptionIfNotSet($argName, $argValue) {
        if (empty($argValue)) {
            throw new DatabaseException("'${argName}' not set");
        }
    }

}


Mysql.php:

class Mysql extends Database {

    private $link = null;

    public function __construct($login, $password, $database, $hostname) {
        parent::__construct($login, $password, $database, $hostname);
        $this->connect();
        $this->selectDatabase();
    }

    public function connect() {
        if (! is_null($this->link)) {
            return;
        }

        $link = @mysql_connect($this->hostname, $this->login, $this->password);
        if (! $link) {
            throw new DatabaseException(
                sprintf(
                    'Cannot connect to database. mysql_connect() to %s with login %s fails',
                    $this->hostname,
                    $this->login
                )
            );
        }
    }

    public function selectDatabase() {
        $ret = @mysql_select_db($this->database, $this->link);
        if (! $ret) {
            throw new DatabaseException("Cannot select database {$this->database}");
        }
    }

}


application.php:

try {
    $db = new Mysql('root', '', 'magic', 'localhost');
    print('ALL PASSED'); //for debugging

} catch (DatabaseException $ex) {
    print('redirect to custom error page will go here');
}

Code Snippets

class DatabaseException extends Exception {
}
abstract class Database {
    protected $login;
    protected $password;
    protected $database;
    protected $hostname;

    public function __construct($login, $password, $database, $hostname) {
        // NB: password not checked and may be empty
        $this->throwExceptionIfNotSet('login', $login);
        $this->throwExceptionIfNotSet('database', $database);
        $this->throwExceptionIfNotSet('hostname', $hostname);

        $this->login    = $login;
        $this->password = $password;
        $this->database = $database;
        $this->hostname = $hostname;
    }

    private function throwExceptionIfNotSet($argName, $argValue) {
        if (empty($argValue)) {
            throw new DatabaseException("'${argName}' not set");
        }
    }

}
class Mysql extends Database {

    private $link = null;

    public function __construct($login, $password, $database, $hostname) {
        parent::__construct($login, $password, $database, $hostname);
        $this->connect();
        $this->selectDatabase();
    }

    public function connect() {
        if (! is_null($this->link)) {
            return;
        }

        $link = @mysql_connect($this->hostname, $this->login, $this->password);
        if (! $link) {
            throw new DatabaseException(
                sprintf(
                    'Cannot connect to database. mysql_connect() to %s with login %s fails',
                    $this->hostname,
                    $this->login
                )
            );
        }
    }

    public function selectDatabase() {
        $ret = @mysql_select_db($this->database, $this->link);
        if (! $ret) {
            throw new DatabaseException("Cannot select database {$this->database}");
        }
    }

}
try {
    $db = new Mysql('root', '', 'magic', 'localhost');
    print('ALL PASSED'); //for debugging

} catch (DatabaseException $ex) {
    print('redirect to custom error page will go here');
}

Context

StackExchange Code Review Q#2395, answer score: 5

Revisions (0)

No revisions yet.