0

主要问题还是一样的:

这是我的项目

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

测试.php

<?php

echo $variable;


?>

index.php

<?php

$variable = "<br>index";

echo 'test<br>';

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

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;
                    }
            }
        }
    }
}

它就像一个魅力,输出是:

test

index

如果我想将此功能封装在这样的函数中:

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);

我懂了:

( ! ) Notice: Undefined variable: variable in C:\wamp\www\sandbox\test1\test.php on line 3

有什么线索吗??

4

2 回答 2

0

Ok, a friend help me to solve it out.

It's was a really dumb error. The problem was when I made the require, it was made in the function scope.

For my solution I do something like this:

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;
}
于 2013-07-19T18:35:00.280 回答
0

您的脚本在浏览文件时可能会在尚未定义test.php之前打开index1.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;
                    }
            }
        }
    }
}
}
于 2013-07-18T20:00:17.930 回答