Creating a Custom Library

Offical Documentation:

Custom Libraries allow you to write reusable code when you need to call some things from multiple extentions.

Libraries don't register themselves so if you want them autoloaded you'll need to register your classes in the libraries folder with jloader.

In short if I call my lib 'Yay' then I will have a file called /libraries/Yay/helper.php

class YayHelper
    public static function sayWooh()
        return "wooh!";

A System Plugin

And a system plugin that registers the library - this will autoload all the classes in the library that follow the naming convention as detailed in the doc linked above.

A system plugin requires a minimum 2 files.

Location: /plugins/system/loadcustomlibrary.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="system">
    <name>Load Custom Library</name>
    <description>Register custom library with Joomla's autoloader</description>
        <filename plugin="loadcustomlibrary">loadcustomlibrary.php</filename>

Location: /plugins/system/loadcustomlibrary.php

class plgSystemLoadcustomlibrary extends JPlugin
    public function onAfterInitialise()
        JLoader::registerPrefix('Yay', JPATH_LIBRARIES . '/yay');

Now anywhere in Joomla I can use any of my functions from the helper like this:

echo YayHelper::sayWooh();

The benefits are that you only need to make sure your library name isn't a copy of one of the things already in the Joomla libraries folder. Joomla will only load the helper class if you are using it because it is autoloaded. You still get the simplicity of being able to call you function/method anywhere without having to import something each time. You can break your functions up into classes if you have a lot of them which may help with reusability and maintainability etc. Having your own library is a nice technique to know if you want to use some code all over the place.