0

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 ?

4
  • 1
    because you are reading $files as array and sending it as a string. just add one new line under loadClass file. if(!is_array($files)){ $files = array($files)} or when you are calling your files pass it in array like. $load->loadClass(['class_template.php']) Commented Apr 7, 2018 at 5:07
  • I would recommend looking into PSR0 or PSR4 auto loading of classes. Commented Apr 7, 2018 at 5:10
  • 1
    @AshishPatel thanks,now I know how different array between class and pure function Commented Apr 7, 2018 at 5:28
  • @ArtisticPhoenix I know that,but I just want to require 5 or 6 class files,so I decide make an require function file Commented Apr 7, 2018 at 5:29

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

1 Comment

I will try it,wait for a moment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.