I'm working in a project using mySQL and SQLite DB. My goal is to make the code able to work with both database types. I developed the code for mySQL, using the mysqli
class, and now I want to modify it to make it also work with SQLite3
objects.
SQLite3
class implements the same methods as mysqli
(at least, those than I use), but sometimes using different names. For example, mysqli::fetch_array()
becomes SQLite3::fechArray()
. So I'm extending SQLite3 and SQLite3Result
classes to implement the methods I need. For example:
class SQLite3ResultEx extends SQLite3Result {
public function fetch_array() {
return parent::fetchArray();
}
}
The problem is that I don't know how to do for extending the response for SQLite3
class to get my own SQLite3Response
extended class. I mean:
class SQLite3Ex extends SQLite3 {
public function __construct($filename, $flags = null, $encryption_key = null) {
return parent::__construct($filename, $flags, $encryption_key);
}
public function query($query) {
$res = parent::query($query);
return $res;
}
}
Now $res
is an SQLite3Result
object, but I need it becomes a SQLite3ResultEx
object (my own extended class).
One way would be to abstract it a bit further, instead of extending SQLite3Result
, you could create a new class that would accept a SQLite3Result
object, provide its own methods which then proxy to the result.
class myResults
{
/**
* @var SQLite3Result
*/
protected $result;
/**
* Constructor
* @param SQLite3Result $result
*/
public function __construct(SQLite3Result $result)
{
$this->result = $result;
}
/**
* Fetch array of results
* @return array
*/
public function fetch_array()
{
return $this->result->fetchArray();
}
// Any other methods you need
}
Then your SQLite3Ex::query
method becomes:
/**
* Preform query
* @param string $query
* @return myResults
*/
public function query($query)
{
$res = parent::query($query);
return new myResults($res);
}
You could try and create $res as a specific parameter of your subclass and reference it directly with
$this->res = ...
so that your subclass looks like:
class SQLite3Ex extends SQLite3 {
private $res;
public function __construct($filename, $flags = null, $encryption_key = null) {
return parent::__construct($filename, $flags, $encryption_key);
}
public function query($query) {
$this->res = parent::query($query);
return $res;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.