Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composer/PSR - How to autoload functions?

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?

like image 270
mpen Avatar asked Jun 11 '14 19:06

mpen


People also ask

How do you autoload with Composer?

After you create the composer. json file in your project root with the above contents, you just need to run the composer dump-autoload command to create the necessary autoloader files. These will be created under the vendor directory. Finally, you need to include the require 'vendor/autoload.

How do you autoload in PHP?

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.

What does Composer dump-autoload do?

Composer dump-autoload: The composer dump-autoload will not download any new thing, all it does is looking for all the classes and files it needs to include again.

How does PHP autoload work?

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.


1 Answers

You can autoload specific files by editing your composer.json file like this:

"autoload": {     "files": ["src/helpers.php"] } 

(thanks Kint)

like image 138
mpen Avatar answered Sep 23 '22 13:09

mpen