I'm using the Joomla Database Framework package for an internal project.

To initiate a Database instance, I'm using the following:

$db = Joomla\Database\DatabaseDriver::getInstance($options);

The $options variable would look something like this:

$options = array(
    'host' => 'localhost',
    'user' => 'XXXXX',
    'password' => 'XXXXXXX',
    'database' => 'XXXXX',

Any function that has a database query will require the getInstance which is perfect fine, however I do not wish to define the $options in every single function.

How would I overcome this?

Would it be better to create a wrapper class/function and call that instead?

  • Better to use wrapper class as you already said. – Sasi varna kumar Aug 18 '15 at 17:55

I ended up creating a wrapper class and function as per the below:

class Database
    protected $db;

    private function _connect()
        $dbFactory = new Joomla\Database\DatabaseFactory;

            $this->db = $dbFactory->getDriver(
                    'host'      => 'localhost',
                    'user'      => 'XXXXX',
                    'password'  => 'XXXXX',
                    'database'  => 'XXXXX',
                    'prefix'    => 'jos_'
        catch (Exception $e)
            die('There was a problem connecting to the database');

        return $this->db;

    public function connect()
        return $this->_connect();

And then in any function where I was to call this, I use:

$dbo    = new Database;
$db     = $dbo->connect();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.