Freight Calculator Example

The examples we have shown so far in this chapter have been contrived to help illustrate the features of PHP. In this section, we apply these techniques and features discussed in this chapter to improve the freight calculator example we introduced in Chapter 4.

Review of the FreightCalculator

The FreightCalculator class defined in Example 4-9 is used to calculate the cost of delivering an online order. Example 4-9 also define the AirFreightCalculator class that redefines the two protected member functions that do the work of calculating the cost components: perKgTotal( ) and perCaseTotal( ).

FreightCalculator objects, including AirFreightCalculator objects, are constructed with two parameters, the total weight and the number of cases that make up a delivery:

    function _  _construct($numberOfCases, $totalWeight)
    {
        $this->numberOfCases = $numberOfCases;
        $this->totalWeight = $totalWeight;
    }

We also defined the CaseCounter class in Example 4-8 that extends the simple UnitCounter class defined in Example 4-4. A CaseCounter object can be used to accumulate units that make up a delivery and calculate the total weight and number of cases required to pack the order. The following example shows how a CaseCounter is created and used:

// Access to the CaseCounter class definition require "example.4-8.php"; // Create a CaseCounter object where there are // 12 units to a case, and each unit weights 1.2 Kg. $myOrder = new CaseCounter(12, 1.2); // Add 5 bottles $myOrder->add(5); ...

Get Web Database Applications with PHP and MySQL, 2nd Edition 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.