Test whether a file exists anywhere in the include path

后端 未结 5 2210
时光取名叫无心
时光取名叫无心 2021-02-20 12:25

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

5条回答
  •  你的背包
    2021-02-20 12:51

    As of PHP 5.3.2 there is the option to use the stream_resolve_include_path() function whose purpose is to

    Resolve [a] filename against the include path according to the same rules as fopen()/include() does.

    If 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:

    function my_autoloader($classname) {
        $found = stream_resolve_include_path($classname . '.specialversion.php');
        if ($found !== FALSE) {
            include $found;
        }
    }
    

提交回复
热议问题