1

I create a functions.php in my project's root folder. My project has some other folder like inc and in this folder there are 2 folder like folder-1 and folder-2. So when I create a php file like something.php, I include this to my functions.php file like include_once 'inc/folder-1/something.php'. But when I create many of php file, every time I need to include or require this files to my function file.

Is there any way to include all php file together dynamically?

Like, I create..

something-1.php in folder-1

something-2.php in folder-1

something-3.php in folder-1

nothing-1.php in folder-2

nothing-2.php in folder-2

nothing-3.php in folder-2

Now I include this file to my function file by writing like this..

include_once 'inc/folder-1/something-1.php';

include_once 'inc/folder-1/something-2.php';

include_once 'inc/folder-1/something-3.php';

include_once 'inc/folder-2/nothing-1.php';

include_once 'inc/folder-2/nothing-2.php';

include_once 'inc/folder-2/nothing-3.php';

Is there any solution to avoid this manual including system?

Like, when I create a new file in those folder, this will automatically include in functions.php file, and when I delete this, there will be no impact on my project...

2

3 Answers 3

0

You can use glob

For folder-1

foreach (glob("inc/folder-1/*.php") as $yourfilename)
{
    include_once $yourfilename;
}

For folder-2

foreach (glob("inc/folder-2/*.php") as $yourfilename)
{
    include_once $yourfilename;
}
Sign up to request clarification or add additional context in comments.

1 Comment

this is a rather basic approach to a problem that has been solved by autoloading
0

First create a file name it something like 'allfiles.php' then include all the files you want include to the file example. include 'inc/folder-1/something.php' follow by all the file up to the last nothing.php, now save it when ever you want include just include the "allfiles.php" all the files will be included hope it helps?

Comments

0

You can set the auto_prepend_file directive in your php.ini file,

http://php.net/manual/en/ini.core.php#ini.auto-prepend-file

reference : PHP auto include

make sure before removing that the file has no dependency on it

Comments

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.