PHP use array to require php files

天涯浪子 提交于 2020-01-06 06:35:55

问题


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";
        }
    }
}

But when I use

$load = new Core;
$load->loadClass('class_template.php');

it doesn't work, can anyone help me to find the error ?


回答1:


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');


来源:https://stackoverflow.com/questions/49704047/php-use-array-to-require-php-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!