HomePHP Page 10 - Building an Extensible Menu Class
Extending Yourself - PHP
All the examples you've seen thus far have used the same standard API defined within the Menu object. However, this assumes one important thing - that a database table (in the format described) has already been created and populated with menu records. In case this is an unreasonable assumption for your specific requirements, you might consider adding a few method calls to add and delete nodes respectively.
// other methods
// function: add a record to the menu table
function create_node($label, $link, $parent)
$this->query("INSERT INTO $this->table(label, link, parent) VALUES
('$label', '$link', '$parent')");
// function: remove a record from the menu table
$this->query("DELETE FROM $this->table WHERE id = '$id'");
You might also want to consider developing a simple
administration interface to these method calls, so that users can easily modify the menu tree via a GUI.
And that's about all for the moment. In this article, you expanded your knowledge of PHP's OOP capabilities by actually using all that theory to build something useful - a menu widget which can be used to describe the relationships within a hierarchical menu system, independent of how the menu is visually presented.
If you work with menu systems, whether on a Web site, within a Web application or on an embedded system, you might find this object a handy tool in your next development effort. If you're a novice programmer struggling to understand how OOP can make your life easier, I hope this article offered some pointers, as well as some illustration of how object-oriented programming works. And if you don't fit into either of those categories - well, I hope you found it interesting and informative, anyway.
See you soon!
Note: All examples in this article have been tested on Linux/i586 with PHP4, HIERmenus 4.0.12, and FolderTree 2.0. HIERmenus and FolderTree copyright their respective authors. Examples are illustrative only, and are not meant for a production environment. YMMV!