【问题标题】:PHP dynamic requirePHP动态需求
【发布时间】:2013-07-18 02:21:59
【问题描述】:

主要问题还是一样:

这是我的项目

/
| 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

有什么线索吗??

【问题讨论】:

  • 不确定它是否与自动加载有关。您在第 3 行执行$app-&gt;get,但未定义 $app。
  • $app 它总是在 index.php 中定义,奇怪的是当我手动执行 require 它的工作时。当它是动态的时候不是。手动 = 已定义 $app 动态 = 未定义 $app
  • @Laurent 我做了一些测试,但问题不在框架中,尽管现在我认为它是 'in' php, fml.

标签: php require


【解决方案1】:

您的脚本在浏览文件时可能会在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;
                    }
            }
        }
    }
}
}

【讨论】:

  • 嗯我似乎不可能,因为我在 index1.php 中调用了函数 rTest,所以文件本身打开了,我在声明 $variable 后调用了函数
【解决方案2】:

好的,朋友帮我解决一下。

这是一个非常愚蠢的错误。问题是当我提出要求时,它是在函数范围内提出的。

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

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

【讨论】:

    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    相关资源
    最近更新 更多