本文介绍了PHP使用数组来要求php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到此答案关于需要多个php文件,我想使用类,像这样
I have saw this answer about require multiple php files,I want to do it use class,like this
class Core
{
function loadClass($files)
{
$this->files = func_get_args();
foreach($files as $file) {
require dirname(__FILE__)."/source/class/$file";
}
}
}
但是当我使用
$load = new Core;
$load->loadClass('class_template.php');
它不起作用,任何人都可以帮助我找到错误吗?
it doesn't work, can anyone help me to find the error ?
推荐答案
您应将$this->files
传递给foreach
. $files
是一个局部变量和一个字符串. $this->files
是一个实例变量和一个数组.
You should pass $this->files
to foreach
. $files
is a local variable and a string. $this->files
is a instance variable and an array.
class Core {
function loadClass() { // there is no need for `$files` here
$this->files = func_get_args();
foreach($this->files as $file) { // $this->files not $files
require dirname(__FILE__)."/source/class/$file";
}
}
}
$load = new Core;
$load->loadClass('class_template.php');
这篇关于PHP使用数组来要求php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!