How can inheritance from the mysqli class be utilized to improve the design of a database connection class in PHP?
Inheritance from the mysqli class can be utilized to improve the design of a database connection class in PHP by allowing the database connection class to inherit all the properties and methods of the mysqli class, reducing code duplication and promoting code reusability.
class DatabaseConnection extends mysqli {
public function __construct($host, $username, $password, $database) {
parent::__construct($host, $username, $password, $database);
if ($this->connect_error) {
die('Connection failed: ' . $this->connect_error);
}
}
}
// Example usage
$db = new DatabaseConnection('localhost', 'username', 'password', 'database_name');
Keywords
Related Questions
- How can JOIN be used to link multiple tables in a MySQL query?
- Are there specific coding conventions or standards for PHP similar to those for Java, and how can they be beneficial in avoiding errors?
- What potential pitfalls should PHP developers be aware of when using the strtotime function for date calculations?