How can I attach a script to multiple other scripts?

55 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

It's always better to have a single entry point for your scripts so that all the common files can be included there. You can use 'autoloader', but even that needs to be 'included' in all these files to work fine. Try to store similar files in the same directory. For eg: keep all your controller files in the controller folder, config files in the config folder.