0

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).

Kevin Nagurski
  • 1,889
  • 11
  • 24
Pepin
  • 3
  • 2
  • 1
    I can recommend a good read: http://php.net/manual/en/intro.pdo.php – Michael Apr 21 '15 at 08:09
  • Yeah, I understand. The right way is to use my own PDO class, so this assures all the methods are available independently the database I use. I'll try doing this. Thank you very much for your answer. – Pepin Apr 21 '15 at 09:31
  • Actually I suggest to use the PDO classes built into PHP. Why would you write it your own when it's ready to use? – Michael Apr 21 '15 at 15:48
  • This is not that I meant. I was talking about to do a class who uses PDO classes (maybe extending it), not creating a full PDO class. This way, I can add methods to change the target database at any moment. – Pepin Apr 22 '15 at 08:39

2 Answers2

1

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);
}
Kevin Nagurski
  • 1,889
  • 11
  • 24
-1

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;
    }
}
MuppetGrinder
  • 234
  • 1
  • 8
  • Thanks for your answer, but I don't understand what're you suggestting. The problem is not to get $res, but making $res becomes a SQLiteResultEx object (now it's a SQLiteResult class object). – Pepin Apr 21 '15 at 08:37