Learning OOP PHP, simple MySQL connection class. 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.

&lt;?php
class Mysql
{
        private $user;
        private $pass;
        private $data;
        private $host;

        public function __construct($user,$pass,$data,$host)
        {
                $this-&gt;user = $user;
                $this-&gt;pass = $pass;
                $this-&gt;data = $data;
                $this-&gt;host = $host;
                $this-&gt;process();
        }

        /* INTERFACE */
        private function process()
        {
                if($this-&gt;verifyNullFields()==true)
                {
                        if($this-&gt;verifyDatabaseConnection()==true)
                        {
                                if($this-&gt;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');
                }
        }

        /* IMPLEMENTATIONS */
        private function verifyNullFields()
        {
                if($this-&gt;user != NULL)
                {
                        if($this-&gt;data != NULL)
                        {
                                if($this-&gt;host != NULL)
                                {
                                        return true;
                                }
                                else
                                {
                                        return false;
                                }

                        }
                        else
                        {
                                return false;
                        }
                }
                else
                {
                        return false;
                }
        }
        private function verifyDatabaseConnection()
        {
                $link = @mysql_connect($this-&gt;host,$this-&gt;user,$this-&gt;pass);
                if(!$link)
                {
                        return false ;
                }
                else
                {
                        return true;
                }
        }
        private function verifyDatabaseExist()
        {
                $db = @mysql_select_db($this-&gt;data);
                if(!$db)
                {
                        return false;
                }
                else
                {
                        return true;
                }
        }
}
?&gt;
&lt;?php
$m = new Mysql("root","","magic","localhost");
?&gt;
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');
}