To add a global function you just need to declare a function early in the lifecycle where you are not in a class - all functions per se are global in php. A reasonable place to do so that would not get overridden would be outside the class declaration in a system plugin. Any functions there will be added to the global space during the joomla initialisation - disabling the plugin will stop the functions being loaded. If you are going to do that then you should make sure your functions are called something that will not collide with other functions. This is not a good practice but whatever.
What I would recommend is that you use static methods instead and register your class in the libraries folder with jloader. The doc is here: https://docs.joomla.org/Using_own_library_in_your_extensions
In short if I call my lib Pf then I will have a file called /libraries/pf/helper.php
<?php
class PfHelper
{
public static function sayWooh()
{
return "wooh!";
}
}
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.
/plugins/system/loadcustomlibrary.xml
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="system">
<name>Load Custom Library</name>
<version>1.0</version>
<description>Register custom library with Joomla's autoloader</description>
<files>
<filename plugin="loadcustomlibrary">loadcustomlibrary.php</filename>
</files>
</extension>
/plugins/system/loadcustomlibrary.php
<?php
class plgSystemLoadcustomlibrary extends JPlugin
{
public function onAfterInitialise()
{
JLoader::registerPrefix('Pf', JPATH_LIBRARIES . '/pf');
}
}
Now anywhere in Joomla I can use any of my functions from the helper like this:
echo PfHelper::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.
return $row[$row];
-- I don't think that's right. You've got clashing variable names. Change the name of the function argument to$rowNumber
and doreturn $row[$rowNumber]
.