O'Reilly logo

Upgrading to PHP 5 by Adam Trachtenberg

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required