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.
<?php
class Mysql
{
private $user;
private $pass;
private $data;
private $host;
public function __construct($user,$pass,$data,$host)
{
$this->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');
}
}
/* IMPLEMENTATIONS */
private function verifyNullFields()
{
if($this->user != NULL)
{
if($this->data != NULL)
{
if($this->host != NULL)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
private function verifyDatabaseConnection()
{
$link = @mysql_connect($this->host,$this->user,$this->pass);
if(!$link)
{
return false ;
}
else
{
return true;
}
}
private function verifyDatabaseExist()
{
$db = @mysql_select_db($this->data);
if(!$db)
{
return false;
}
else
{
return true;
}
}
}
?>
<?php
$m = new Mysql("root","","magic","localhost");
?>
$this->data != NULL
is the same as!$this->data
. Think of a better structure for the verifyNullFields() method, something simple but more readable could beif (!$this->user || !$this->data || !$this->host) return false; else return true;
\$\endgroup\$