There is no function auto-loader for functions. You have four realistic solutions:

  1. Wrap all functions into namespacing classes (context appropriate). So let's say you have a function called string_get_letters. You could add that to a class called StringFunctions as a static function. So instead of calling string_get_letters(), you'd call StringFunctions::get_letters(). You would then __autoload those namespaced classes.

  2. Pre-load all functions. Since you're using classes, you shouldn't have that many functions, so just pre-load them.

  3. Load functions prior to using them. In each file, require_once the function files that are going to be used in that file.

  4. Don't use functions in the first place. If you are developing OOP code (which it seems like you are anyway), there should be little to no need for functions at all. Everything you would need a function (or multiple) for, you could build in a OO manner and avoid the need for functions.

Personally, I'd suggest either 1, 2 or 4 depending on your exact need and the quality and size of your codebase...


If you are using Composer in your Project, you can add a files directive to the autoload section.

This will than actually generate a require_once in the autoloader, but it feels like real autoloading, because you dont have to take care of that.
Its not lazy loading though.

Example taken from Assetic:

"autoload": {
        "psr-0": { "Assetic": "src/" },
        "files": [ "src/functions.php" ]
    }

I read something a while back about an ugly hack that caught fatal errors and tried to include and execute the missing function(s), but I definitely wouldn't go that road.

The closest thing you have is the __call() magic method, which is sort of a __autoload() for methods, not functions. It might be good enough for your needs; if you can afford to call a class and require each different function separately. Since PHP 5.3.0, you also have __callStatic().

An example using __callStatic():

class Test
{
    public function __callStatic($m, $args)
    {
        if (function_exists($m) !== true)
        {
            if (is_file('./path/to/functions/' . $m . '.php') !== true)
            {
                return false;
            }

            require('./path/to/functions/' . $m . '.php');
        }

        return call_user_func_array($m, $args);
    }
}

Test::functionToLoad(1, 2, 3);

This would call the functionToLoad() function defined in ./path/to/functions/functionToLoad.php.


Well, as usual there is a PECL extension for that:

  • automapPECL

(via: http://phk.tekwire.net/joomla/support/doc/automap.htm)

It's supposed to autoload functions as well as classes. Which however doesn't work with the current PHP interpreter yet.

(An alternative option btw, is generating stub functions that load and run namespaced counterparts.)

That being said. Autoloading is not universally considered a good practice. It leads to overly fractured class hierarchies and object happiness. And the real reason PHP has autoloading is because include and dependency management systems are inmature.