File structure and code optimization is very important to keep systems maintainable. One way to do this is through the use of an include files that serves as a container that holds all of your other include files. Normally, you would have a shared set of includes as part of your source code. For example you can have a couple of PHP files, index.php for the home page and another file called categories.php. In both of these files you would include your shared set of files as so:
<?php
// These would be the include statements for index.php, categories.php, or any other *.php that you have
include(‘includes/functions.inc’);
include(‘includes/constants.inc’);
include(‘includes/mail.inc’);
include(‘includes/database.inc’);
// Add the rest of your include files here
// The rest of your code goes here
?>
These four files can be your code library that you keep together for easy access. When naming your include files, use the *.inc extension to distinguish these include files from the your *.php files that users will actually access directly through the browser. You could include these same four files for every single one of your *.php files but what happens when your library base expands and you need to add more include lines? Having to return to your *.php files to add additional includes later on is truly a hassle. This is why I recommend that you use an include file that includes your entire library. This main include file is like a container that holds all your include files, or objects. When you want to use your library inside your *.php files, you would include just the single include file instead of every single file in the library.
You can name this main include file library.inc and it would look like this:
<?php
include(‘includes/functions.inc’);
include(‘includes/constants.inc’);
include(‘includes/mail.inc’);
include(‘includes/database.inc’);
// Add the rest of your include files here
?>
This main include file looks basically the same as index.php and categories.php. The main difference is that this for those two pages, you can include the file library.inc instead of all the files separately like before.
Now for your index.php and categories.php files, they would look like this now:
<?php
include(‘includes/includes.inc’);
// The rest of your code goes here
?>
Now you just have a single line of code in your *.php files instead of the four or more line that you had previously. Reducing the lines of code makes your source code easier to read. This also makes your code simpler to maintain. In the future, if you want to add more include files to your library, you just have to add them to the library.inc file and then all of your *.php files will have access to your entire library. You will never have to return to all of your *.php file and update them with any additional includes files from your library. All that you will ever need is just that one single line in them that includes the library.inc file!
0 Responses to “PHP: Organize Code With an Include of Includes”
Leave a Reply