You could, for example user PDO instead of mysqli for its benefits. Or, you could simiply typecast like
public function setMysqli(mysqli $mysqli) {
$this->mysqli = $mysqli;
}
A better way around would be to have those kind of stuff in the constructor as it is initialization:
class MySQLConnector implements DatabaseConnector {
private $connection;
/**
* Initialize connector instance.
* Not using OO API of mysqli here because it's junk anyways - you should use PDO.
* @param $host The host of the SQL server.
* @param $username The user for the database.
* @param $password The password of the user.
* @param $database The database to be used.
* @throws RuntimeException if connection fails.
*/
public function MySQLConnector($host, $username, $password, $database) {
if (!$this->connection = mysqli_connect($host, $username, $password, $database)) {
throw new RuntimeException(mysqli_connect_error());
}
}
/**
* Retrieve the connection reference for use.
* The field $connection will always be a valid mysqli instance since
* it is already initialized by the constructor.
* @return A connection handle to the database for use with persistence operations.
*/
public function getConnection() {
return $this->connection;
}
}