Laying out your files

Through the use of include() and require(), PHP is very flexible about how you lay out the code in your scripts, and you would be surprised about how much variation there is amongst programmers when asked how best to lay out their files.

As with many programming elements, you need to find what suits you best, so here's a selection of the most popular techniques:

  1. Separate the header and footer into separate files, header.php and footer.php, create a file, include.php, which contains standard code used across all pages. Each page on the site then occupies one file, which includes include.php, header.php, adds its own content, then includes footer.php

  2. Put all code for all pages into one file, using a switch/case to pick the right code to execute. This is popular in small, administration screens where you will see URLs like admin.php?action=edit.

  3. Separate groups of functions into various include files such as db.php and session.php. Each page on the site is one file, and includes the necessary function files for what it needs to do.

  4. In an object-oriented site, make each class its own PHP file and include them as necessary in each script.

Each makes sense in one way, so consider carefully. Option 1 provides enough header and footer reusability to allow you to keep design separate from content. That is, if you want the header code to change for all pages, you simply edit header.php.

Author's Note: It is a common myth that option 1 will use up a lot more RAM on your computer if you have a PHP opcode cache in place "as it will cache header.php multiple times in each script". This is not true, and hasn't been true for years. In the early days of Zend Engine 1 (way before PHP 4.0 was released), yes, PHP would attempt to merge scripts together before execution, but that was overly problematic. PHP 4 and PHP 5 both keep the scripts separate, and therefore they are cached separately too.

Option 2 only really works well for scripts with a small number of options, and even then should really be split up into separate scripts. The reason for this is that having multiple, unrelated actions in one scripts overcomplicates things, especially if the actions are long. Make your life simple - you know it makes sense!

Option 3 makes PHP work more like other programming languages such as C and Java, and this is no bad thing - declaring up front what the script uses is actually beneficial as outside programmers coming in can clearly see its dependencies. If you go with this option, try not to get too over-eager - if you only have one or two functions in an include file, you are being too specific and need to clump some together.

Option 4 is often just overkill and arises from programmers trying to make PHP work too much like C++ and Java. Yes, if the classes are entirely distinct from one another, you should make them separate files as this allows you to easily re-use classes from one project to another. However, if they are classes that work together you should store them together, as it helps keep your directories that little bit cleaner and also means you only need to include() one file to import several classes.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Directory structuring >>

Previous chapter: Popular IDEs

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.