This forum has a very diverse member base consisting of many different skill levels, intelligence levels and personality types from all over the world. When you ask a question or make a comment, you'll get many different answers from various members who are all looking at a particular issue from different angles. I don't feel that judging the entire forum based on the answers from any individual member is logical. Further, I believe that these characteristics are what make devnetwork the great place it is. We all accept each other here, we are all friends, but we certainly say what is on our mind.
Although the terms "ignorance" and "laziness" are ambiguous, I do believe this is a valid argument because in almost all cases a superior method of error suppression can be used in place of @. In all of the code I have ever written, I have maybe used it 2 or 3 times. There can be serious side effects when using @ for error suppression. This includes severe performance degradation, propagation, (using @include 'file.php'; will suppress errors for the entire file and every file it includes as well), very unpleasant debugging experiences and a few things I'm sure I am forgetting. I do believe that with this knowledge, continuing to use @ for error suppression would indeed be signs of ignorance and/or laziness.
In regards to the original post, there is no need to use @. Here is an example of how it could be done:
Code: Select all
public function create($host, $user, $pass, $dbas, $port) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbas = $dbas;
$this->port = $port;
try {
switch (MYSQL_DBTYPE) {
case 'MYSQLI':
$this->dbClass = $this->mysqli();
break;
case 'MYSQL':
default:
$this->dbClass = $this->mysql();
}
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
}
return is_object($this->dbClass) ? $this->dbClass : false;
}
private function mysql() {
if (!is_resource(($this->dbcon = mysql_connect("{$this->host}:{$this->port}", $this->user, $this->pass)))) {
throw new Exception("Failed to create MySQL database connection. #" . mysql_errno() . ' - ' . mysql_error());
}
if (!mysql_select_db($this->dbas, $this->dbcon)) {
throw new Exception("Failed to create MySQL database connection. #" . mysql_errno() . ' - ' . mysql_error());
}
return new dbmysql($this->dbcon);
}
private function mysqli() {
if (!is_object(($this->dbcon = mysqli_connect($this->host, $this->user, $this->pass, $this->dbas, $this->port)))) {
throw new Exception("Failed to create MySQLi database connection. Error: " . mysql_errno() . ' - ' . mysql_error());
}
return new dbmysqli($this->dbcon);
}
When executing a query, the following method for error detection could be used:
Code: Select all
public function query($query)
{
$result = (!mysqli_multi_query($this->link_id, $query)) ? false : true;
if (!$result) {
$this->result = false;
trigger_error("Error: " . mysqli_errno($this->link_id) . ' ' . mysqli_error($this->link_id), E_USER_WARNING);
} else {
$this->store_result();
}
return $result;
}