Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoloader for functions

Last week I learned that classes can be included in your project by writing an __autoload() function. Then I learned that using an autoloader isn't only a technique but also a pattern.

Now I'm using the autoloader in my project and I've found it very very useful. I was wondering if it could be possible to do the same thing with functions. It could be very useful to forget about including the right PHP file with functions inside it.

So, is it possible to create a function autoloader?

like image 214
Shoe Avatar asked Jan 19 '11 15:01

Shoe


People also ask

How we can implement the autoloading of classes?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

How does PHP autoloader works?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.

What is composer generated autoloader definition?

Autoloading: The classmap DirectiveFor each file, Composer will make a list of classes that are contained in that file, and whenever one of those classes is needed, Composer will autoload the corresponding file. Let's quickly revise the composer. json file to demonstrate the classmap autoloader. 1.

What is the relation of Namespacing and autoloading PHP files?

Basically it says: "inside this directory, all namespaces are represented by sub directories and classes are <ClassName>. php files." Autoloading is PHP's way to automatically find classes and their corresponding files without having to require all of them manually.


2 Answers

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...

like image 147
ircmaxell Avatar answered Sep 21 '22 07:09

ircmaxell


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" ]     } 
like image 43
ivoba Avatar answered Sep 22 '22 07:09

ivoba