I am trying to figure out how to handle the mysql connection within a class. Without always wrapping it in an if statement. Here is an example to help you understand what i am trying to do.
class Database {
protected $conn;
protected $password;
protected $username;
protected $servername;
public function Database(){
$this->conn = new mysqli($this->servername,$this->username,$this->password);
}
public function scriptA(){
if ($this->conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else{
//do script A
return $result;
}
}
public function scriptB(){
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else{
//do script B
return $result;
}
}
}
As you can see if i were to add more scripts i would have to add in a lot of if catches to check it has been connected. is there something like a function exception handler that php has? IE function foo throws exception?