I am using a lot of include files to include my 191 files of with the collection of functions, classes ect.
A problem for me is I really dislike editing the include files its gets a big mess and sometime i just forget to include something.
Therefore I was wondering, is there a include function for php or own made library that includes all the php files in a folder or even better in its own folder + all its sub-folders.
These things make life much easyer and flexible.
You could just have a 'meta-include' file which has the individual include statements, and then you only include that one single file in your scripts.
Of course, the auto-loading versions in the other answers here would be more efficient. While PHP's pretty fast at loading/parsing, 191 individual files to load for every request would add up pretty quick.
I usually require as first application_top.php with this:
If you have a standard between the Class Names and the files you can use the
__autoload()
function. It will save you a lot of includes.http://php.net/manual/en/language.oop5.autoload.php
You can use the following function i just created:
START EDIT
This is the new version of the same function. Now it allows you to specify folders as
folder
orfolder/
without crashing. Also now it loads all files in all folders and subfolders.END EDIT
You can also specify a specific extension if you want to load for example only
.txt
files in a folder you can execute is like this:load_folder('folder/', '.txt');
. Remember that someone think that this is somehow insecure. Before using this function inside a business site, look for more opinion about the topic. Notice also that if some of your files are regarding classes you could use the__autoload()
PHP native function to let PHP call the class where it is really needed (lazy loading).References: