How to get a list of all included PHP files

How to get a list of all included php files

When working with PHP, as your website or web application grows in size so will the number of files you'll include in order for it to perform a task for your users. This is especially true when using different open-source software whether that is via composer or manually. For a given PHP script where there are many includes (and required files), PHP provides developers with a function so you can view this list of includes and requires in an array format, using, get_included_files. Let's explore how this works, and why it might be useful for you to view all the files that have been loaded on a script-by-script basis.

PHP's get_included_files and get_required_files functions

These two PHP functions are what you can use to view all the loaded files from a particular script. The "get_required_files" function is actually just an alias of "get_included_files" and will perform exactly the same way. The function accepts no parameters and returns an array of the included files.

get_included_files(): array

As this returns an array, the easiest way to view the list is by looping the array or using "print_r" or "var_dump". Both print_r and var_dump are commonly used by PHP programmers to debug PHP software and for this example will highlight the used files in the below sample code. The get_included_files function will return an array of all files including the file that has been called it and any files that are included (or required) more than once will only appear once in the list.

# This file is called test.php

include 'testabc.php';
include 'testabc.php'; # if included again, it won't appear twice in the list
include_once 'views/testdef.php';

$getIncludeList = get_included_files();

foreach ($getIncludeList as $file) {
 echo "$file\n";
}

# Outputs
/path/test.php
/path/testabc.php
/path/views/testdef.php

You can see from the example above how useful this could be when loading large web applications, especially those that depend on open-source software. It could be that for a particular area of your website, users are getting bottlenecked with poor performance. Using the get_included_files would allow you to quickly view and explore areas that are loading in a lot of PHP code. It's possible that the unnecessary loading of files (which increases the system's memory usage) is one factor in bad performance. Scaling this up, when your website has spikes or large amounts of traffic, you can see this could quickly become an issue.

This function is helpful under loads of different scenarios, such as when you're debugging. It can be great to find all the files included in performing a particular task and help you in the location where the bug might occur. By checking each included file, you might spot where the issue is. The opposite could be true, maybe a file with logic that you require isn't included in the runtime of the PHP application, such as an important config file, and therefore might be the result of your bug.

It is not uncommon for developers to use get_included_files as part of their performance monitoring of PHP applications. By examining the array of files returned from this function, you may be able to optimise the autoloading and/or structure of the application. This can be very helpful when working on a PHP project that has been built by someone else or when using a piece of open-source software for the first time. A lot of open-source software such as Magento has a large codebase, and adding new modules could be daunting. Using the get_included_files function can help put you in a good position when working with large PHP codebases.

Another common use of this function is mapping out large or complex applications. By using this function, it is possible to visually see the dependencies of each file, which could assist you in refactoring, or understanding the application's flow. By doing this, it can help you document the application, especially when attempting to add code comments and document blocks also known as DocBlocks.

If in the list of included files, are files that shouldn't be loaded in and are not used for the script's purpose, be sure to remove them. Remember you will get different results based on the file you call it from. A PHP controller might for example include files more than a PHP model if using an MVC structure.

Conclusion

Using the get_included_files function is a great way to debug your code and improve its performance. Use it to file which scripts include others to help you find performance issues (maybe due to high memory usage) or to easily give you a list so you can hop from file to file to find a bug.

  • The function returns an array of included files used within the file it is called from
  • The script which calls the function will also be included in the list of files
  • Duplicate includes or requires will only appear once in the list

Senior PHP developer. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience, and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all