PHP使用数组来要求PHP文件
我看到大约需要多个php文件, 我想用类来做,像这样PHP使用数组来要求PHP文件,php,require,Php,Require,我看到大约需要多个php文件, 我想用类来做,像这样 class Core { function loadClass($files) { $this->files = func_get_args(); foreach($files as $file) { require dirname(__FILE__)."/source/class/$file"; } } } 但是当我使用 $load =
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');
它不工作,有人能帮我找到错误吗?您应该将$this->文件传递给foreach$files是一个局部变量和一个字符串$此->文件是一个实例变量和一个数组
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');
因为您将$files作为数组读取,并将其作为字符串发送。只需在loadClass文件下添加一行新行。如果是_array$files{$files=array$files}或者在调用文件时,像这样在数组中传递它$load->loadClass['class_template.php']我建议您研究PSR0或PSR4类的自动加载。@AshishPatel谢谢,现在我知道类和纯类之间的数组有多大不同了function@ArtisticPhoenix我知道,但我只需要5或6个类文件,所以我决定制作一个require函数文件我会试试,等一下