[英]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.