PHP动态要求

PHP动态要求,php,require,Php,Require,主要问题还是一样的: 这是我的项目 / | index.php |-test1 | test.php test.php <?php echo $variable; ?> 如果我想将此功能封装在如下函数中: function rTest($full_path, $adm_extension){ $paths = array(); if (is_dir($full_path)) { if (($handle = opendir($full_

主要问题还是一样的:

这是我的项目

/
| index.php
|-test1
   | test.php
test.php

<?php

echo $variable;


?>
如果我想将此功能封装在如下函数中:

function rTest($full_path, $adm_extension){
    $paths = array();
    if (is_dir($full_path)) {
        if (($handle = opendir($full_path))) {
            while ($file = readdir($handle)) {
                if (is_file($full_path . '/' . $file)) {
                    $item_path = $full_path . '/' . $file;
                    $extension = pathinfo($item_path, PATHINFO_EXTENSION);
                        if (in_array($extension, $adm_extension)) {
                             $paths[] = $item_path;
                        }
                }
            }
        }
    }
    return $paths;
}

foreach(rTest($full_path, $adm_extension) as $path){
    require $path;
}
index1.php

$variable = "<br>index";

echo 'test<br>';

$full_path = dirname(__FILE__)."/test1";
$adm_extension = array( "php" );

function rTest($full_path, $adm_extension){
if (is_dir($full_path)) {
    if (($handle = opendir($full_path))) {
        while ($file = readdir($handle)) {
            if (is_file($full_path . '/' . $file)) {
                $item_path = $full_path . '/' . $file;
                $extension = pathinfo($item_path, PATHINFO_EXTENSION);
                    if (in_array($extension, $adm_extension)) {
                        require $item_path;
                    }
            }
        }
    }
}
}

rTest($full_path, $adm_extension);

任何线索???

您的脚本在浏览文件时,可能会在
index1.php
之前打开
test.php
,因此
$variable
尚未定义

测试它:

function rTest($full_path, $adm_extension){
if (is_dir($full_path)) {
    if (($handle = opendir($full_path))) {
        while ($file = readdir($handle)) {
            if (is_file($full_path . '/' . $file)) {
                $item_path = $full_path . '/' . $file;
                $extension = pathinfo($item_path, PATHINFO_EXTENSION);
                    if (in_array($extension, $adm_extension)) {
                        echo $item_path.'<br />';
                        require $item_path;
                    }
            }
        }
    }
}
}
函数rTest($full\u path,$adm\u extension){
if(is_dir($full_path)){
if(($handle=opendir($full_path))){
而($file=readdir($handle)){
if(is_file($full_path./'.$file)){
$item_path=$full_path.'/'.$file;
$extension=pathinfo($item\u path,pathinfo\u extension);
if(在数组中($extension,$adm\u extension)){
回显$item_路径。“
”; 需要$item\u路径; } } } } } }
好的,一位朋友帮我解决这个问题

这真是个愚蠢的错误。问题是当我提出要求时,它是在功能范围内提出的

对于我的解决方案,我会这样做:

function rTest($full_path, $adm_extension){
    $paths = array();
    if (is_dir($full_path)) {
        if (($handle = opendir($full_path))) {
            while ($file = readdir($handle)) {
                if (is_file($full_path . '/' . $file)) {
                    $item_path = $full_path . '/' . $file;
                    $extension = pathinfo($item_path, PATHINFO_EXTENSION);
                        if (in_array($extension, $adm_extension)) {
                             $paths[] = $item_path;
                        }
                }
            }
        }
    }
    return $paths;
}

foreach(rTest($full_path, $adm_extension) as $path){
    require $path;
}

不确定它是否与自动加载有关。您正在第3行执行
$app->get
,但是$app没有定义。它总是在index.php中定义的$app,奇怪的是当我手动执行require它的工作时。当它不是动态的时候。手动=已定义$app Dynamic=未定义$app@Laurent我做了一些测试,但问题不在框架中,尽管现在我认为它在php中,fml.hmm我似乎不可能,因为我在index1.php中调用函数rTest,所以文件本身被打开,我在声明$variable后调用函数
function rTest($full_path, $adm_extension){
if (is_dir($full_path)) {
    if (($handle = opendir($full_path))) {
        while ($file = readdir($handle)) {
            if (is_file($full_path . '/' . $file)) {
                $item_path = $full_path . '/' . $file;
                $extension = pathinfo($item_path, PATHINFO_EXTENSION);
                    if (in_array($extension, $adm_extension)) {
                        echo $item_path.'<br />';
                        require $item_path;
                    }
            }
        }
    }
}
}
function rTest($full_path, $adm_extension){
    $paths = array();
    if (is_dir($full_path)) {
        if (($handle = opendir($full_path))) {
            while ($file = readdir($handle)) {
                if (is_file($full_path . '/' . $file)) {
                    $item_path = $full_path . '/' . $file;
                    $extension = pathinfo($item_path, PATHINFO_EXTENSION);
                        if (in_array($extension, $adm_extension)) {
                             $paths[] = $item_path;
                        }
                }
            }
        }
    }
    return $paths;
}

foreach(rTest($full_path, $adm_extension) as $path){
    require $path;
}