JROB JROB - 4 months ago 56
PHP Question

PHP: How much slower is glob() vs listing specific files?

I have quite a few

files in my
directory. As the application is updated, these may change, more may be added, and some may be removed.

I have a file that is loaded every page load which includes all necessary files. Right now, I am doing this:

$files = glob($site->settings->root_path.'/classes/*.{php}', GLOB_BRACE);

foreach ($files as $file) {

Is this much slower than specifying each file individually? Like:


The reason I ask is because I think I remember something from a while back that led me to believe that iterating through files/folders was a painfully slow process, but I have no way of knowing for sure.

EDIT: If anyone has a better suggestion than
, I'd be happy for the input.


Answering your question: yes, it will definitely be slower, just because you first have to get the list of files, and then execute require_once, that will make another access to file system to check if the file actually exists.

But this is a wrong way to do in my opinion. What you're trying to do is achieved with autoloading.

Basically, you have to define a function which will try to load a file from a file system, when a script wants to access a class that is not defined. This function then maps the name of the class (with namespaces) to a file on a file system and tries to load it. This way you don't have to worry about how many files you have in your folder, and how often they are added or removed.