Search code examples
phpoopautoloader

How can I attach a script to multiple other scripts?


I have a directory in my project which contains 10 php files.

files/
    file1.php
    file2.php 
    file3.php
    .
    .
    .

I want to add the following line to all of those 10 php files:

require_once('/config.php');

I can open them manually and add this ^ line into all of them. But in that case, if the path of config.php changed, then I have to modify all those 10 php files.

Anyway, Isn't there any better manner to include that line into all files of a directory? I suspect it may be possible by using autoloader. Am I right?


Solution

  • Autoloaders are not intended to load files, but to load class definitions (which just happen to be inside of a file.

    Therefore, whether you can use autoloader to load those files or not, will depend on what those files actually contain.

    As for how autoloader would work, you can look up the PHP's manual or PSR-4 doc. If those are what you are looking for, in that case it would be actually easier to just use the autoloader, that is bundled with composer instead of making your own.