使用include函数读取文件夹中的所有php文件


read all php file inside folder using include function

我尝试读取文件夹中的所有文件。我创建了一个文件夹sample,在这个文件夹中我创建了php文件testing.php。这个文件看起来像这样

我在示例文件夹之外创建了另一个文件。我将其命名为details.php

我把这些代码放在details.php

function include_all_php($folder){
    foreach (glob("{$folder}/*.php") as $filename)
    {
        echo $filename;
        include($filename);
    }
}
$file =  __DIR__."/sample";
include_all_php($file);
showx();

testing.php文件中,我将这些代码放入

function showx(){
    echo "heroo";
}

我收到这个错误

Fatal error: Call to undefined function shox()

我回显$filename。它显示了我的文件testing.php,但为什么我不能调用函数showx()?我的代码有问题吗?

之前尝试一下:

function endsWith($haystack, $needle) {
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}
function include_all_php($folder){
$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($folder));
    foreach ($dir as $filename)
    {
        if(endsWith($filename, ".php")){
            include $filename;
        }
    }
}
include_all_php('/path/');