[英]PHP Scope Issue Between Included File Variable, Global Function, And Classes
[英]How to include files inside a function and still grant the functions of included file global scope in php
我被一些抽象和组织错误咬住了,我决定通过使用名为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');
但是问题在于,现在上述方法包含的文件的所有功能都被赋予了上述方法load
的范围。 现在我不能使用包含文件的任何功能。 每当我使用上述包含的文件中的任何功能时,都会收到警告,提示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所述,请检查路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.