Category Archives: php

PhpDocumentor in 5 minutes

Help Point

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

  • You are on a UNIX based machine, which includes Linux and Mac OS X.
  • You are documenting object oriented rather than procedural code

Install

PhpDocumentor is part of PEAR. Install is easy:

sudo pear install phpdocumentor

Documenting your source code

The rules:

  • One class per file
  • One docbook block for the class, not for the file
  • Filename is `.php`

For the class:

  • short description
  • code example
  • @author

For each attribute:

  • short description
  • @var

For each method:

  • short description
  • @param
  • @return
  • @see

Example class with documentation

Accumulator.php

    <?php
    
    /**
     * An instance of this class represents a counting machine
     *
     * <code>
     * require_once 'Accumulator.php';
     * 
     * $acc = new Accumulator( 10 );
     * 
     * $acc->addNum( 20 );
     * 
     * echo $acc->getTotal();
     * </code>
     *
     * @author Glen Scott <glen @ glenscott.co.uk>
     */
    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


Want advice on building better software? Join my mailing list…


Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Closures in PHP 5.3

A good article from IBM’s developerWorks on the new functional programming features in the upcoming release of PHP 5.3:

What’s new in PHP V5.3, Part 2: Closures and lambda functions

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Closing PHP tag considered harmful

A good tip from Zend, who recommend omitting the closing PHP tag (?>) when your files contain pure PHP code. This can prevent additional whitespace being inserted into the script output. Extra whitespace can cause problems, for example, if you are trying to set your own custom HTTP headers: nothing should be output before the call to the header function. Otherwise, this classic error will occur:


Cannot modify header information - headers already sent by (output started at myinclude.php:16) in myscript.php on line 15

Zend Framework: Documentation

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Your top places, according to Flickr

I’ve been messing around with a little Flickr application this afternoon which shows your top places. Here are mine:

Give it a try and post a comment with your number one place:

Flickr Top Places

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Installing PEAR on Mac OS X 10.5 Leopard

PEAR was removed by Apple from 10.5 for one reason or another. Here’s how to get it back:

Pre-requisties

Make sure you have a php.ini file in /etc/. This isn’t available by default. To create one enter the following:

sudo cp /etc/php.ini.default /etc/php.ini

PEAR Install

  • cd
  • curl -O http://pear.php.net/go-pear
  • sudo php go-pear

This starts the interactive install script.

  • Press Enter to start install
  • Press Enter to use no HTTP proxy
  • Type 1 and press Enter
  • Enter /usr/local/pear as the path and press Enter
  • Press Enter to confirm the install directories
  • Press Enter to install PEAR_Frontend_Web-beta,
    PEAR_Frontend_Gtk2, MDB2 by default

The installer will also alter the include_path in your php.ini file to include the new installation directory, so just press Enter when prompted

The PEAR Package Manager

To make using the command line tool easier, we are going to modify our shell path. Add the following line to the end of your ~/.profile file:

export PATH="$PATH:/usr/local/pear/bin"

After saving your .profile file, log into a new terminal and type the following to confirm that everything is working

pear version

If all is well, you should see the following output (or similar):

PEAR Version: 1.7.2
PHP Version: 5.2.5
Zend Engine Version: 2.2.0
Running on: Darwin glen-scotts-macbook.local 9.3.0 Darwin Kernel Version 9.3.0: Fri May 23 00:49:16 PDT 2008; root:xnu-1228.5.18~1/RELEASE_I386 i386

For more information on using the manager to install and maintain packages, see the PEAR manual for the command line installer.

Clean up

rm go-pear

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

The Problem with PEAR

I have a love-hate relationship with PHP’s class repository (and answer to Perl’s CPAN), PEAR. Sometimes you can find very useful, well documented and cleanly implemented packages. And obviously re-using such components is a big-win. Sadly, the quality of PEAR varies wildly and frequently I stumble upon useful looking packages that have little or, worse, no documentation. This situation, regardless of the underlying code, puts me off more than anything.

It’s odd, as writing examples and documentation is a pre-requisite for new PEAR package submissions. The question is, should the PEAR developers be more strict on what they let in?

PEAR :: The PHP Extension and Application Repository

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn