Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include() all PHP files from a directory?

Tags:

include

php

In PHP can I include a directory of scripts?

i.e. Instead of:

include('classes/Class1.php');
include('classes/Class2.php');

is there something like:

include('classes/*');

Couldn't seem to find a good way of including a collection of about 10 sub-classes for a particular class.

like image 400
occhiso Avatar asked Sep 28 '22 07:09

occhiso


People also ask

What is include () in PHP?

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

Can we include PHP file using Cimbine () function?

Instead of writing all our code in a single file, PHP allows us to INCLUDE other files, so they function as part of the current page, while keeping them separate. This is very useful for organization and not repeating code for things such as footers and navigation.

How do I include two PHP files?

The include() statement is used to include a php file in another file. This way you can write a piece of code in a php file and can use it to multiple files through include() statement.


1 Answers

foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}
like image 472
Karsten Avatar answered Oct 16 '22 10:10

Karsten