HomePHP Page 3 - Building an Extensible Menu Class
What's On The Menu? - PHP
So you know the theory behind OOP, but don't really understandits applications? Well, it's time to take objects out of the classroom andinto the real world - this article demonstrates how OOP can save you timeand effort by building a PHP-based Menu object to describe therelationships in a hierarchical menu tree. And since the proof of thepudding is in the eating, it then combines the newly-minted Menu objectwith some of the most popular JavaScript menu systems available online toshow you how cool objects really are.
So that's the theory. Let's now spend a few minutes discussing the rationale behind the Menu object I plan to build.
Conceptually, a Web site can be considered as a combination of two things: menus and content. Menus are used to organize and classify the type of content, and to offer one or more navigational paths to specific content modules.
Now, although a menu may be visually presented in a number of different ways, there are certain common elements present in every menu:
1. Most menus are broken into levels, with each level more focused than the last; this hierarchical structure is sometimes referred to as a "menu tree".
2. Every menu tree consists of nodes connected to each other by branches.
3. A node may have one or more children, but can have only one parent.
Using these common principles, it is possible to build a Menu object which exposes certain generic methods. These methods will have nothing to do with the visual presentation of the menu tree; rather, they provide a simple API to various menu attributes and relationships, and can be used by client- or server-side scripts which are more closely connected to the presentation layer.