I'm writing an autoload function and in the inner logic of it would like to test whether a certain file exists somewhere in the path prior to including it.
This is the logic:
If a file named $className'.specialversion.php'
exists anywhere in the include path include it. Otherwise, let other autoloaders take care of including a file for this class.
At the moment I just do: @include($calculatedPath);
I'm not sure if it's a good approach to include and suppress the error. I would rather check if the file exists (somewhere in the include path) prior to including it.
My question is:
- Can I test for existence of a file anywhere in the include path?
- Is it really problematic to do
@include($calculatedPath);
?
Edit
An important accent: I don't know where the file should be. I just want to know whether it exists in one of the directories in the include path. So I can't just do file_exists()
or something like that.
As of PHP 5.3.2 there is the option to use the
stream_resolve_include_path()
function whose purpose is toIf the file exists on one of the include paths, then that path (including the file name) will be returned. Otherwise (i.e. the file was not on any of the include paths) it will return
FALSE
.Relating this to your needs, your autoloader might look something like:
I've written a function that can test it nicely
You should avoid the error supressor operator
@
.Update
If your class could be in a number of directories, you could...
If you do decide to traverse all folders looking for the class, and it becomes a bottleneck (benchmark it), you could benefit from caching the class name to file location mapping.
I would use
file_exists
rather than a warnings-suppressed include.Then you'll have to iterate through the
include_path
:As a simple resolution, you should a test within the SPL-function file_get_contents() by setting the second argument to TRUE.
--Rolf