PHP
  Home arrow PHP arrow Page 5 - Writing Self-Documenting PHP Code
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Writing Self-Documenting PHP Code
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2002-04-15

    Table of Contents:
  • Writing Self-Documenting PHP Code
  • Speaking In Tongues
  • Drilling Deeper
  • I, Robot
  • Tonight's Menu
  • Different Strokes
  • Closing Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Writing Self-Documenting PHP Code - Tonight's Menu


    (Page 5 of 7 )

    Now, what you saw on the previous page was just an example. Let's apply PHPDoc to an actual PHP class, one created by me a while back and pulled out of our archives for just this purpose, and see what happens.


    <?php /** * 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 * relationships, and can be used by client- or server-side scripts * which are more closely connected to the presentation layer. * * @author Melonfire <melonfire@mail.com> * @version 1.0 * @since 1.0 * @access public * @copyright Melonfire */ class Menu { /** * The hostname of the server hosting the database. * * @var string * @access public * @since 1.0 * @see set_database_parameters() */ var $hostname; /** * The username required for logging in to the database server. * * @var string * @access public * @since 1.0 * @see set_database_parameters() */ var $user; /** * The password required for logging in to the database server. * * @var string * @access public * @since 1.0 * @see set_database_parameters() */ var $password; /** * The name of the database containing the required tables. * * @var string * @access public * @since 1.0 * @see set_database_parameters() */ var $db; /** * The name of the table containing the menu information. * * @var string * @access public * @since 1.0 * @see set_database_parameters() */ var $table; /** * The constructor for the Menu class sets some default values for the database parameters * * @access public * @since 1.0 * */ function Menu() { $this->set_database_parameters("localhost", "me", "bs49h5634", "apps","menu"); } /** * This function is used to set the database parameters as specified by the user. * * @param string $hostname The host name of the database server * @param string $user The username for accessing the database server * @param string $password The password for accessing the database server * @param string $db The name of the database to be accessed * @param string $table The name of the table that stores the menu * @access public * @since 1.0 * @see query() * */ function set_database_parameters($hostname, $user, $password, $db, $table) { $this->hostname = $hostname; $this->user = $user; $this->password = $password; $this->db = $db; $this->table = $table; } /** * This is a generic function to execute a query that is passed as a parameter * * @param string $query The query to be executed * @return mixed $ret * @access public * @since 1.0 * */ function query($query) { // connect $connection = mysql_connect($this->hostname, $this->user, $this->password) 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; } /** * This function returns the id of the parent of a node * * @param int $id The id of the node who parent has to be returned by function * @return int $id * @access public * @since 1.0 * */ 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]; } } ?>
    Here's the PHP script which hooks this class file up to PHPDoc and actually generates some documentation:

    <html> <head> </head> <body> <?php // where are the PHPDoc files? // alter this as per your setup define("PHPDOC_INCLUDE_DIR", "/usr/local/apache/htdocs/phpdoc/"); // system linebreak sequence // alter this as per your setup define("PHPDOC_LINEBREAK", "\r\n"); // include PHPDoc files include("prepend.php"); // instantiate a PHPDoc object $doc = new Phpdoc; // set application name $doc->setApplication("Menu"); // source file location // alter this as per your setup $doc->setSourceDirectory("/usr/local/apache/htdocs/phpdoc/Menu/"); // destination directory for generated docs // alter this as per your setup $doc->setTarget("/usr/local/apache/htdocs/phpdoc/Menu/docs/"); // template location // alter this as per your setup $doc->setTemplateDirectory("/usr/local/apache/htdocs/phpdoc/renderer/htm l/te mplates/"); // source file suffixes $doc->setSourceFileSuffix( array ("php", "inc") ); // parse $doc->parse(); // and render $doc->render(); ?> </body> </html>
    And here's what it all looks like:

    More PHP Articles
    More By Harish Kamath, (c) Melonfire


     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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