What are some best practices for error handling and resource management in custom database classes in PHP?
When working with custom database classes in PHP, it is important to implement proper error handling and resource management to ensure the stability and security of your application. This includes using try-catch blocks to handle exceptions, closing database connections after use, and freeing up resources to prevent memory leaks.
class CustomDatabase {
private $connection;
public function __construct($host, $username, $password, $database) {
$this->connection = new mysqli($host, $username, $password, $database);
if ($this->connection->connect_error) {
throw new Exception("Connection failed: " . $this->connection->connect_error);
}
}
public function query($sql) {
$result = $this->connection->query($sql);
if (!$result) {
throw new Exception("Query error: " . $this->connection->error);
}
return $result;
}
public function close() {
$this->connection->close();
}
public function __destruct() {
$this->close();
}
}
Related Questions
- What are the limitations of using hacks or workarounds for complex text formatting tasks in PHP, and when should developers consider building a custom tokenizer/parser instead?
- How can one determine the difference between "&&" and "||" in PHP?
- What are the best practices for installing and managing new styles or themes in a PHP forum like PHPBB?