Assembling the Application

Finally, you can integrate the classes and templates into a complete application. This section creates a dual web and command-line program that shares the same set of code but abstracts out the details, such as processing the different sources of input data.

Creating the Web Version

The web version gets its input from a form embedded in the HTML template’s header, as shown in Example 10-19.

Example 10-19. Web-enabled address book application

// Configure format-specific details
$input = $_POST;
$template = new htmlTemplate;

// Set mode
if (isset($input['mode'])) {
    $mode = $input['mode'];
} else {
    $mode = false;

try { 
    // Create address book
    $ab = new addressBook;
    // Load data into Person
    $person = new Person($input);
    // Add person, if necessary
    if ($mode =  = 'add') {
    // Return results
    // Print page
} catch (Exception $e) {
    $ob = ob_start( );
    print $e;
    error_log(ob_get_clean( ));

At the top of Example 10-19, you set a few configuration variables. Since this is the web version, the $input comes from $_POST and the $template is an htmlTemplate. Later, you will set $input and $template to different values in the command-line version.

The $mode variable, which controls whether you should add a new person, is assigned using the mode element of the input source.

Once everything is set up, create the addressBook and Person objects. Use the $input array to configure Person.

Now, if the $mode ...

Get Upgrading to PHP 5 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.