PhpDocumentor in 5 minutes

February 18, 2009 by Glen. Filed under php.

Introduction

This is a quick and dirty guide to the absolute minimum you need to get up and running with PhpDocumentor.

One of the slightly off-putting aspects of PhpDocumentor is the amount of tags (`@`) that are available, which can initially be overwhelming. e.g. see PEAR example:

PEAR sample file

This guide shows you the most important tags that you need to have in your documentation by means of an actual example: `Accumulator.php`.

Assumptions

Install

PhpDocumentor is part of PEAR. Install is easy:

    sudo pear install phpdocumentor

Documenting your source code

The rules:

For the class:

For each attribute:

For each method:

Example class with documentation

Accumulator.php

    <?php

    /**
     * An instance of this class represents a counting machine
     *
     * 
     * require_once 'Accumulator.php';
     *
     * $acc = new Accumulator( 10 );
     *
     * $acc->addNum( 20 );
     *
     * echo $acc->getTotal();
     * 
     *
     * @author Glen Scott 
     */
    class Accumulator {
        /**
         * The running total
         *
         * @var int
         */
        private $_number;

        /**
         * Create an instance, optionally setting a starting point
         *
         * @param int $initial an integer that represents the number
         *                     to start counting from
         * @access public
         */
        public function __construct( $initial = 0 ) {
            $this->_number = $initial;
        }

        /**
         * Adds a number to the running total
         *
         * @param int an integer to add to the running total
         */
        public function addNum( $num ) {
            $this->_number += $num;
        }

        /**
         * Returns the current total
         *
         * @return int returns the current running total
         * @see Accumulator::$number
         */
        public function getTotal() {
            return $this->_number;
        }
    }

Creating your documentation

phpdoc –filename Accumulator.php –target docs

The `”docs”` directory is created and some HTML files are generated. Load `docs/index.html` into your browser to read your documentation.

PhpDoc sample page

Possible issues

`”phpdoc: command not found”`. This means that the documentation generator script is not in your path. Run `”pear config-get bin_dir”` and add this directory to your shell’s `$PATH` environment variable.

Links

  • Share/Bookmark

Tags: , ,

Commentary

  1. Nick says:

    How to get phpDocumentor to work with PHP5.3 namespaces?

  2. Frederik Cheeseman says:

    Hi Glen,

    I don think it’s a problem but nevertheless I would ask your permission to put your link http://www.glenscott.co.uk/2009/02/18/phpdocumentor-in-5-minutes/ as a reference on my web page concerning the use of PhPDocumentor. You did me remember the installation procedure for phpdocumentor via PEAR ;-) So thanks ;-)

    Yours,

    Frederik in Brussels

  3. Glen says:

    No problem Frederik, I’m glad you found the tutorial useful!

Add a comment

Follow comments to this post by subscribing to the comment feed.