How can I autoload helper functions (outside of any class)? Can I specify in composer.json
some kind of bootstrap file that should be loaded first?
Asked
Active
Viewed 2.8k times
67

mpen
- 272,448
- 266
- 850
- 1,236
2 Answers
98
You can autoload specific files by editing your composer.json
file like this:
"autoload": {
"files": ["src/helpers.php"]
}
(thanks Kint)

Jeff Puckett
- 37,464
- 17
- 118
- 167

mpen
- 272,448
- 266
- 850
- 1,236
-
12Note that this file will always be included when calling the autoload file - so this better be quick, or every php script using it will be affected. – Sven Jun 11 '14 at 20:58
-
1This was helpful, thanks. Just as a recomendtion, check if the function exist first : if (!function_exists('myfunction')) – CTala Apr 01 '17 at 06:36
-
How do I load a functions.php file containing only functions from a Git repo in a package? I have access to edit the the package's composer.json file. – Donnie Ashok Mar 16 '18 at 10:57
-
@DonnieAshok Not sure what Git has to do with it. You can't load functions remotely if that's what you're asking. Just change `src/helpers.php` to `path/to/functions.php`. If that's not clear, you might want to ask a new question. – mpen Mar 16 '18 at 17:04
-
5This isn't a suitable answer for people who have a PSR-4 autoload pattern already set up and want to autoload functions in the same manner as classes. – Jay Bienvenu Jun 26 '18 at 19:32
-
@JayBienvenu You're allowed to use this in addition to PSR-4 autoload. If that's not acceptable to you, the only other option I'm aware of is to use public static methods instead of raw functions. `__autoload`/`spl_autoload_register` does not support loading functions. – mpen Jun 27 '18 at 00:13
-
I ran into a similar situation. Having a function `foobar` in a `functions.php` is **only possible** to load via composer, if the file is outside of `src` AND it uses checks, if functions already exist. If its inside `src`, you will get an error like `The autoloader expected class "App\functions"...`. If you dont check if your function(s) already exist, you will run into `Cannot redeclare ...` issues. – k00ni Feb 11 '19 at 12:31
-
@k00ni That doesn't sound right. `src` isn't special, and you will only get a "Cannot declare ..." if you try loading it twice. "expected **class** `App\functions`" is also fishy -- these aren't classes we're talking about. – mpen Feb 11 '19 at 22:19
-
Hi @mpen, i could not found a good source how to "autoload" files with composer, **which contain only functions**. I don't want to use classes for these. My current workaround is described in my comment above. Is there a good source how to do it "the right way"? – k00ni Feb 12 '19 at 09:41
-
1My answer describes exactly how to do that. Just include `vendor/autoload.php` somewhere and composer will take care of the rest. It sounds like you might have a namespace (called `App`) which is causing you troubles. Make sure you use the full-qualified function name if you're using one. – mpen Feb 13 '19 at 20:26
16
After some tests, I have came to the conclusions that adding a namespace to a file that contains functions, and setting up composer to autoload this file seems to not load this function across all the files that require the autoload path.
To synthesize, this will autoload your function everywhere:
composer.json
"autoload": {
"files": [
"src/greetings.php"
]
}
src/greetings.php
<?php
if( ! function_exists('greetings') ) {
function greetings(string $firstname): string {
return "Howdy $firstname!";
}
}
?>
...
But this will not load your function in every require of autoload:
composer.json
"autoload": {
"files": [
"src/greetings.php"
]
}
src/greetings.php
<?php
namespace You;
if( ! function_exists('greetings') ) {
function greetings(string $firstname): string {
return "Howdy $firstname!";
}
}
?>
And you would call your function using use function ...;
like following:
example/example-1.php
<?php
require( __DIR__ . '/../vendor/autoload.php' );
use function You\greetings;
greetings('Mark'); // "Howdy Mark!"
?>

Anwar
- 4,162
- 4
- 41
- 62
-
3That is because your condition is incorrect - you should use FQN for `function_exists()`: `if(!function_exists('You\greetings'))`. It has nothing to do with autloading. – rob006 Nov 04 '18 at 10:23
-
1You can also use `if (! function_exists(__NAMESPACE__ . '\greetings'))` to avoid repeating the FQDN too many times, which makes it easier to move namespaces if necessary during development. – Jason Jun 24 '19 at 15:29
-
1+1 for working example, also if you don't want to specify namespace before each function call just add `use function You\greetings;` to the top of you file – mohas Jun 25 '19 at 09:44