Now that we have an autoloader in place, we can begin to remove all the include calls that only load up class and function definitions. When we are done, the only remaining include calls will be those that are executing logic. This will make it easier to see which include calls are forming the logic paths in our legacy application, and which are merely providing definitions.

We will start with a scenario where the codebase is structured relatively well. Afterwards, we will answer some questions related to layouts that are not so amenable to revision.

First, we will consolidate all the application classes to our central directory location as determined in the previous chapter. Doing so will put them where our autoloader can find them. Here is the general process we will follow:

For our examples, we will assume we have a legacy application with this partial file system layout:

Your own legacy application may not match this exactly, but you get the idea.

Now the problem is that our original file is trying to include the class file from its old location, a location that no longer exists. We need to remove that call from the code:

However, there are likely to be other places where the code attempts to load the now-missing lib/sub/User.php file.

This is where a project-wide search facility comes in handy. We have different options here, depending on your editor/IDE of choice and operating system.

The point is to find all the include calls that refer to lib/sub/User.php. Because the include calls can be formed in different ways, we need to use a regular expression like this to search for the include calls:

If you are not familiar with regular expressions, here is a breakdown of what we are looking for:

(Regular expressions use . to mean any character so we have to specify User\.php to indicate we mean a literal dot, not any character.)

If we use a regular expression search to find those strings in the legacy codebase, we will be presented with a list of all matching lines and their corresponding files. Unfortunately, it is up to us to examine each line to see if it really is a reference to the lib/sub/User.php file. For example, this line might turn up in the search results:

However, clearly it is not the User.php file we are looking for.

Examine each search result line, and if it is an include that pulls in the User class, remove it and save the file. Keep a list of each modified file, as we will need to test them later.

At the end of this, we will have removed all the include calls for that class throughout the codebase.