0

主な問題はまだ同じです:

これは私のプロジェクトです

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

test.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

わかりました、友人がそれを解決するのを手伝ってくれます。

本当にばかげたエラーでした。問題は、require を作成したときに、関数スコープで作成されたことです。

私のソリューションでは、次のようにします。

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 に答える