Not until PHP 8 is released. It will be possible with PHP 8 to have union types, but that doesn't mean it is a good idea.
First of all, you should always enable proper mysqli error reporting. Just add this line before new mysqli():
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
If a query fails then mysqli will throw an exception instead of returning FALSE value.
However, mysqli_query() can also return TRUE for queries that do not return any results e.g. INSERT.
Since PHP 8 you will be able to do union types:
class xyz extends abc {
protected mysqli_result|bool $result;
public function queryExecute(string $query): void {
$this->result = $this->mysqli->query($query);
}
}
In PHP 7.4 you can do this by replacing true with null. After all, you really don't need that boolean value.
class xyz extends abc {
protected ?mysqli_result $result;
public function queryExecute(string $query): void {
$res = $this->mysqli->query($query);
$this->result = true === $res ? null : $res;
}
}
How to write mysqli abstraction library?
It looks like you are trying to build a mysqli abstraction library. PHP already has a much better library called PDO, but if you want to build your own here are some pointers.
Use prepared statements all the time. There's barely any reason to use mysqli_query.
Keep it simple. Don't bother with storing mysqli_result in an attribute. You only need this object to fetch results and then you can discard it.
Always enable error reporting.
Don't expose mysqli_result nor mysqli_stmt objects. Use them only internally.
I have used this class for demonstration purposes how you can start with such library.
class DBClass extends mysqli {
public function __construct(
$host = null,
$username = null,
$passwd = null,
$dbname = null,
$port = null,
$socket = null
) {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
$this->set_charset('utf8mb4');
}
/**
* Executed prepared statement
*
* @param string $sql SQL query with placeholders e.g. SELECT * FROM users WHERE Id=?
* @param array $params An array of parameters to be bound
* @return array|null
*/
public function safeQuery(string $sql, array $params = []): ?array {
// prepare/bind/execute
$stmt = $this->prepare($sql);
if ($params) {
$stmt->bind_param(str_repeat("s", count($params)), ...$params);
}
$stmt->execute();
// If the query produces results then fetch them into multidimensional array
if ($result = $stmt->get_result()) {
return $result->fetch_all(MYSQLI_BOTH);
}
// return nothing if the query was successfully executed and it didn't produce results
return null;
}
}
There is no need to store the result in an attribute because we return the array with values immediately. This class fulfils most use cases and is very simple to use.
$db = new DBClass('localhost', 'user', 'pass', 'db_name');
// INSERT
$db->safeQuery('INSERT INTO user(name) VALUES(?)', ['Dharman']);
// You still have access to the parent mysqli object if needed
var_dump($db->insert_id);
//SELECT
$usernames = $db->safeQuery('SELECT * FROM user WHERE name=?', ['Dharman']);
foreach ($users as $user) {
echo $user['id'];
}