Dom’s Blog

Polysyllabic Pretension Pertaining Primarily to Programming

 

Sample of My Code

This code sample is excerpted from a blog post on the same topic.  It is intended to highlight my naming conventions and documentation style, rather than anything else.  Error-handling is rudimentary and backwards-compatible with PHP4 for clarity and because it’s a “Hello World” class after all.

When giving out advice on code, I believe in the school of thought that says “put up or shut up.” It’s difficult for me to respect the code of others when they won’t show their chops, so to speak. Here is me putting up so you can see for yourself if I need to shut up, or if you can learn something of value here. This code is a sample class to, of course, tell the world “Hello!” with a little style.

<?php

/**
 * class Hello
 *
 * This class helps us display our feelings to the world.
 *
 * LICENSE: You are free to do absolutely anything with this
 *          file.
 *
 * @copyright  2009 Domenic R. Merenda
 * @license    Unrestricted, no specific license
 * @version    1.00
 * @link       http://www.edgeprod.com
 * @since      File available since Release 1.00
 * @todo       Take over the world with this file alone
 */

class Hello {

   public $returnText = 'Hello World!';  // Stores the string to return

   /**
    * public function getHelloText($moodId,$smileyId)
    *
    * @param   integer  $moodId     0 = happy, 1 = sad, 2 = silly
    * @param   integer  $smileyId   0 = :), 1 = :(, 2 = 8-)
    * @return  string   returns the switch()-determined value of $moodId
    *                   and $smileyId
    */

    public function getHelloText($moodId,$smileyId) {

        $returnText .= ' '; // Add a space for human readability

        // Add the next sentence based on our mood ($moodId parameter)

        switch($moodId) {

            case 0 : // Happy
               $returnText .= 'I am extremely happy!';
            break;

            case 1 : // Sad
               $returnText .= 'I am so sad!';
            break;

            case 2 : // Silly
               $returnText .= 'I stick cereal in my nose!';
            break;

            // If the parameter is incorrect, return an error

            default :
               return 'Parameter $moodId must be 0, 1, or 2!';
            break;
        }

        $returnText .= ' '; // Add another space for human readability

        // Add a smiley based on the $smileyId parameter

        switch($smileyId) {

            case 0 : // Smiley Face
               $returnText .= ':)';
            break;

            case 1 : // Frownie Face
               $returnText .= ':(';
            break;

            case 2 : // Silly Face
               $returnText .= '8-)';
            break;

            // If the parameter is incorrect, return an error

            default :
               return 'Parameter $smileyId must be 0, 1, or 2!';
            break;
        }

        return $returnText;
    }
}

?>