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') {
        $ab->addPerson($person);
    }
    
    // Return results
    $ab->search($person);
    
    // Print page
    $template->printAll($ab);
} 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 ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.