Browse DevX
Sign up for e-mail newsletters from DevX


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

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

Simplify Hyperlink Maintenance
Easy templates are great, but there's more to a useful Web site than just looks. Pages in useful sites are often heavily linked together. As the site grows, managing the links and keeping them working can become a huge problem, particularly when you need to move pages or folders around. Inheritance makes link maintenance significantly easier, although it does not completely alleviate the problem.

Implementing page inheritance will require a change in link syntax. Typically, a site might use HTML such as the following.

<a href="/folder/page.php">description</a>

In contrast, with the OOP file system, you call a special page() PHP function to create a link:

<? page("page.php", "description"); ?>

Using this function, you no longer need to provide the full path for links; you just specify enough to uniquely identify a page, and the system figures out the rest of the path from context. For a page in the current directory or its parent directories, the filename is sufficient. For example, to link to /bears/pictures.php from /bears/species/black.php, you would write:

<? page("pictures.php", "Pictures"); ?>

For a page in a sibling folder, you'll need to provide the sibling name and the file name. For example, if you wanted to link to /animals/bears/habitat.php from /animals/wolves/species/index.php, you could write:

<? page("bears/habitat.php", "Bear Habitats"); ?>

This is significantly easier than specifying either the full path or a relative path in HTML, which would be /animals/bears/habitat.php or ../../bears/habitat.php. The full path works regardless of where it is linked from, but causes problems if the page you link to ever moves. For example, if you renamed /animals/ to /mammals/, all the links to the /animals/ section would break. Using relative links avoids that problem, but breaks if you move the page you link from. However, using inheritance, neither situation causes a problem.

The page() function works on the same underlying concept as the include() function and its include_path variable—it searches for whatever file name you give it, starting in the current directory and traveling upward through parent directories until it finds something or runs out of places to look. The function itself is fairly simple; it mostly just calls a generic search_parents() function and prints the result as HTML. The search_parents() function does a few sanity checks, then executes the actual search, making sure to keep track of both the real location of the file on disk, and its virtual location on the Web server. The actual details and implementation are mundane, but nevertheless important. Look through the commented inherit.inc file for more detail, in the downloadable code provided with this article.

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