Learning OOP PHP, simple MySQL connection class.
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-&gt;throwExceptionIfNotSet('login', $login);
        $this-&gt;throwExceptionIfNotSet('database', $database);
        $this-&gt;throwExceptionIfNotSet('hostname', $hostname);

        $this-&gt;login    = $login;
        $this-&gt;password = $password;
        $this-&gt;database = $database;
        $this-&gt;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-&gt;connect();
        $this-&gt;selectDatabase();
    }

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

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

    public function selectDatabase() {
        $ret = @mysql_select_db($this-&gt;database, $this-&gt;link);
        if (! $ret) {
            throw new DatabaseException("Cannot select database {$this-&gt;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');
}