Use Include Files to Provide Inheritance
At the core of the OO-FS system is inheritance. What this means, essentially, is that any request for an object or file should search upward through the parent classes of the current page if the requested object or file is not found in the current directory. In this way, subdirectories (or child classes) can inherit the attributes (files) of their parents.
A PHP page commonly requests other files by using PHP's include()
, or HTML's <a href>
tags. I'll start with include()
, because it is the easiest to set up. A simple, though somewhat ugly statement in a .htaccess file
is all you need:
php_value include_path \
This odd-looking code defines a list of directories to search when you call include()
. Each item on the list is separated by colons. The ".
" is the current directory, and "..
" is the parent of the current directory. To go one more level up, use "../..
". You can extend this to as many levels as appropriate, by adding more items with an extra "../
" prepended to each. You should make the list as long as the depth of your site's deepest nested content, so that the most buried content can still inherit from the root.
Also, this introduces a convention. Each include file resides in a php/
subdirectory. The idea here is to keep the files organized, with pages in folder/
, code in folder/php/
, graphics in folder/gfx/
, and downloadable files in folder/files/
To see how the include_path
works, pretend you have a page located at /animals/wolves/index.php
, and that it executes include("header.inc")
. PHP will search for the header.inc
file in several places, until it either finds the file or runs out of places to look. Specifically, it will look here:
If the file isn't in any of those locations, the include() will fail.
A Word of Caution
Before going any further, you may want to protect your code. This scheme places your include files inside of the $DOCUMENT_ROOT folder, which means people can download them and look at the source. Because you may have passwords or other sensitive data in those files, you need a way to prevent unauthorized people from viewing those files. Therefore, give each include file a .inc
extension, and add the following to your .htaccess
Deny from All
That causes requests for your code to fail, keeping your implementation code and passwords safe.
Applying Templates and Libraries
With just the single tool of inheritance via include()
, you can do some powerful things. For example, you can now easily apply templates to entire sections of your site, and easily load functions from libraries without having to care where those libraries are. You can also provide defaults for the entire site and then override them in any folder. To use full-page templates, you only need two lines of code in each page. The header and footer define the common parts of the pages in the site; the main content for each page would go between the header and footer code lines, as shown in the following code fragment:
<? include("header.inc"); ?>
(insert page content here)
<? include("footer.inc"); ?>
It's important to consider that templates don't need to apply to complete pagesyou can easily define smaller parts of the page in exactly the same manner, putting a menu here or a news ticker there. You would write these smaller pieces as separate files, and then simply include()
those pieces from your header or footer. By breaking your pages up into their component parts in this manner, you can either use or override them easily. For example, consider the following file layout; a small site about animals. Table 1 shows the files and a description of each:
Table 1. "Animal" Site Files: The table provides a list of files in the sample "animals" site, along with a description of each.
||Front page of the site
||Top of page template
||Bottom of page template
||Main site news
||Main page about bears
||News about bears, overrides default news
||Main page about lions
||Main page about wolves
||Top of wolf page template, overrides main one
||Bottom of wolf page template
From looking at the first four items in Table 1, you can see that the site provides a default page template, which in turn uses a sub-template to display recent news. Both can be overridden by subdirectories. In this example, the bears/ folder provides its own news widget, which can display news appropriate to bears, rather than the full list of news items displayed by the rest of the site. The lions/ area does nothing special, so it inherits the look and feel of the site's front page. In contrast, the wolves/ section has its own full-page template, giving it a different appearance than every other section.