Autoloading libraries

Libraries can be very useful to add extra functionality to Opencart.
In our implementation we have the 'dbimg' library, which contains a load of functions which are useful to our implementation.

conventional loading a library


// dbimg is a singleton !!!
$dbimg = Dbimg::get_instance($this->registry);

$product_id = $dbimg->getProductId();
return $product_id;

autoloading a library

Autoloading a library is a bit more work, but not that difficult either.

After we created our library, we have to register it. We do this with an modification.
We assume that our library is named 'ourcustomlib.php' as in class 'OurCustomLib'.

The customization looks like this:

<?xml version="1.0" encoding="utf-8"?>
    <name>Register OurCustomLib Lib</name>
    <author>Lord Byron</author>
    <file path="catalog/controller/startup/startup.php">    
$this->registry->set('openbay', new Openbay($this->registry));
            <add position="after">
$this->registry->set('dbimg', new OurCustomLib($this->registry));

What will happen?

How to use this

Let's assume this OurCustomLib has a method named getTheHotJuice, then it works as following:

$juice = $this->ourcustomlib->getTheHotJuice();

That's all there is.

Last update: Tue, 27 Aug 2019 7:40:01