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 $mode ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access