如何在函数中包含文件,并且仍然在php中授予包含文件全局范围的函数


How to include files inside a function and still grant the functions of included file global scope in php

我被一些ABSTRACTION&ORGANISATION bug,我决定使用一个名为Loader的类将文件包含在我的php应用程序中,该类看起来像

class Loader
{
    public $loadedFiles=array();
    public function isLoaded($fileName)
    {
        if(in_array($fileName,$this->loadedFiles))
        return true;
        else 
        return false;
    }
    public function load()
    {
        $fileList=func_get_args();
        foreach ($fileList as $file)
        {
            if(!$this->isLoaded($file))
            {
                $flag=include(ROOT_DIR_PATH.'includes'.DS.$file);
                if($flag)
                {
                    $this->loadedFiles[]=$file;
                }
                else
                return false;
            }
        }
    }
}

现在我可以使用类似的东西在我的应用程序中包含文件

$loader=new Loader();
$loader->load('db.class.php','utility.php','objects.php');

但是问题是,现在由上述方法包括的文件的所有功能都被授予上述方法CCD_。现在我无法使用包含文件的任何功能。每当我使用上面包含的文件的任何函数时,我都会收到一个警告,说undefined function。是否有某种方法可以授予所包含文件的功能全局范围。

看起来问题出在其他地方。我试着模拟你想做的事情,这对我很有效。
include1.php

 <?php
 function tester()
     echo "This is tester'n";
 }

include2.php

<?php
class Loader {
    public function load() {
        $var = include('include1.php');
    }
}
$loader = new Loader();
$loader->load();
tester();

结果是:这是测试仪

不存在任何范围界定问题。正如@JackTurky提到的,检查路径。