PHP
  Home arrow PHP arrow Page 5 - Building an Extensible Menu Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building an Extensible Menu Class
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2001-08-10


    Table of Contents:
  • Building an Extensible Menu Class
  • Back To Class
  • What's On The Menu?
  • Children And Their Parents
  • I Say Method, You Say Madness...
  • Rounding Up The Family
  • Saving My Bookmarks
  • Reaching Higher
  • Collapsing Inwards
  • Extending Yourself

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building an Extensible Menu Class - I Say Method, You Say Madness...
    ( Page 5 of 10 )

    With the database design out of the way, it's now time to begin work on the methods which will interact with the records in the database. Before actually sitting down to code the class, it's a good idea to spend some time listing the important methods, together with their purpose. Here's my initial cut:

    query($query) - execute an SQL query;

    get_children($node) - return a collection of this node's children;

    get_parent($node) - return the identifier of this node's parent;

    get_label($node) - return the name of this node

    get_type($node) - return the node type (leaf or branch)

    is_root_node($node) - is this node at the root level or not?

    These are the essential methods; there may be more, which I will add as development progresses.

    Let's now begin by setting up the class definition:

    <? // menu.class.php // methods to obtain node and tree relationships class Menu { } ?><hr noshade size=1 color=#cccccc></pre></blockquote> Now, since this class will be talking to a database, I need to add a few variables to hold database-specific information. <blockquote><pre><hr noshade size=1 color=#cccccc><? class Menu { // set up some variables with default values // to hold database parameters // hostname, user and password var $hostname; var $user; var $pass; // database and table containing menu data var $db; var $table; } ?>
    PHP makes it possible to automatically execute a specific function when a new instance of a class is spawned. This function is referred to as a "constructor" and must have the same name as the class.

    In this case, I plan to initialize my Menu object with certain default values for the various database parameters. I have the option of assigning these variable-value pairs individually, or writing a method to assign them all in one fell swoop; I pick the latter.

    <? class Menu { // set up some variables // snip // constructor function Menu() { $this->set_database_parameters("localhost", "me", "bs49h5634", "apps", "menu"); } // function: set database parameters // returns: none function set_database_parameters($hostname, $user, $password, $db, $table) { $this->hostname = $hostname; $this->user = $user; $this->password = $password; $this->db = $db; $this->table = $table; } } ?>
    In case you're wondering, the $this prefix provides a convenient way to access variables and functions which are "local" to the class

    Now, all the methods listed above will be querying the database for information. Since the connect-and-query code is common to all of them, I can extract it into a separate method named query().

    <? class Menu { // other methods // function: execute query $query // returns: result identifier function query($query) { // connect $connection = mysql_connect($this->hostname, $this->user, $this->pass) or die ("Cannot connect to database"); // run query $ret = mysql_db_query($this->db, $query, $connection) or die ("Error in query: $query"); // return result identifier return $ret; } } ?>
    All my object methods can now simply use query() to execute SQL queries on the database. Further - if I ever decide to move to another database, I need only update the code in this single function to ensure that my class will continue to work with the new system.{mospagebreak title=Playing With Nodes} With the underlying, database-specific layer in place, I can now begin work on the main object methods. The first of these is also one of the simplest - it accepts a node id and returns the id of the node's parent.

    <? class Menu { // other methods // function: get parent // returns: node id function get_parent($id) { $query = "SELECT parent FROM $this->table WHERE id = '$id'"; $result = $this->query($query); $row = mysql_fetch_row($result); return $row[0]; } } ?>
    An offshoot of this is the is_root_node() method, used to test whether a particular node is at the base of the menu tree.

    <? class Menu { // other methods // function: is this node at the root of the tree? // returns: boolean function is_root_node($id) { if($this->get_parent($id) == 0) { return 1; } else { return 0; } } } ?>
    The get_label() and get_link() methods accept a node id and return the corresponding text and URL from the table.

    <? class Menu { // other methods // function: get label for $id // returns: string function get_label($id) { $query = "SELECT label FROM $this->table WHERE id = '$id'"; $result = $this->query($query); $row = mysql_fetch_row($result); return $row[0]; } // function: get link for $id // returns: string function get_link($id) { $query = "SELECT link FROM $this->table WHERE id = '$id'"; $result = $this->query($query); $row = mysql_fetch_row($result); return $row[0]; } } ?>
    Using the menu table constructed a few pages ago, the code

    <? $obj = new Menu(); echo $obj->get_label(3); ?>
    would return

    Los Angeles


     
     
    >>> More PHP Articles          >>> More By Team Melonfire, (c) Melonfire
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT