Browse DevX
Sign up for e-mail newsletters from DevX


Build an Object-oriented File System in PHP : Page 5

Using a simple file storage convention and some PHP code, you can create a hierarchical file system that mimics many object-oriented concepts, facilitates reuse, and simplifies your Web development efforts.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Putting It All Together
To use the PHP functions described above and otherwise make the inheritance system work, you will need to add a standard include file to each page. This standard include file sets up the basic inheritance system, loads default settings, loads other libraries you expect to use on every page, and loads placeholders you can use elsewhere to override or enhance the default settings. While not strictly necessary, using a standard include file such as the one in this article's downloadable code adds power and flexibility to the OO file system. You will also find details and source to a small working site in the sample code.

You could specifically add a standard include file by writing the code to load it at the top of each page, or add it generically by writing the code at the top of each header template. The included code demonstrates both methods. Despite the extra line per file, it's useful to put the include line in each page, because you can then modify the defaults before the page template starts to display. Simply put this at the top of each page:

<? include("std.inc"); ?>

Then, between this line and the line to include the page header, you can change variables used by the header, and otherwise modify the style before it begins to display. You'd typically use such modifications to set the page title, or set other parameters used by the main template.

You'll find some small extras included with the source. These are not necessary to implement the OO file system, but you may find them useful. A brief description of each follows:

  • Menus: Three files implement a system for side menus: The side_menu.inc file is generic code to handle a menu definition. The file menu_style.inc tells the code what HTML you want to display for each type of menu item, while menu.inc contains the main menu definition. You can easily define the menu contents per-folder or per-section on a large site. The default HTML style uses CSS-friendly divs to make applying themes easy, and the sample site includes a simple CSS style that demonstrates how to apply a theme to the menu items.
  • Automatic Forwarding: In forward.inc you'll find functions to direct a browser automatically from one place to another. This is particularly useful if you want to move content without breaking incoming links from other sites. Combine this with replacements for the default 404 document's "Page not found" message and a bit of string manipulation, and you can redirect entire sections with little effort.
  • Modification Time: This is useful if you want each page to show the last time it was changed, without having to do so manually. Just include mod_time.inc in your template, and call its mod_time() function where you want the information to appear.
I have other components available for this system too, such as an image gallery and a basic content management system. Feel free to contact me for more information.

Scott Scriven is a freelance open-source hacker who enjoys dabbling in a wide variety of technologies, and creating beauty through simplicity. He strives, in all aspects of life, for three things: to learn, to love, and to play.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date