18.4. Manipulating measurements
上一页 | 第 18 章 Zend_Measure | 下一页

## 18.4. Manipulating measurements

Parsing and normalization of input, combined with output to localized notations makes data accessible to users in different locales. Many additional methods exist in `Zend_Measure` components to manipulate and work with this data, after it has been normalized.

- `Convert`

- `Add and subtract`

- `Compare`

- `Calculate differences`

- `Manually change values`

- `Manually change types`

### 18.4.1. Convert

Probably the most important feature is the conversion into different units of measurement. The conversion of a unit can be done any number of times using the method `convertTo()`. Units of measurement can only be converted to other units of the same type. Therefore, it is not possible to convert (e.g.) a length into a weight, which would allow bad programming practice and allow errors to propagate without exceptions.

例 18.10. Convert

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Weight');

    $locale = new Zend_Locale('de');
    $mystring = "1.234.567,89";
    $unit = new Zend_Measure($mystring,Zend_Measure_Weight::POUND, $locale);

    print "Kilo:".$unit->convertTo(Zend_Measure_Weight::KILOGRAM);
    print "Ton:".$unit->convertTo(Zend_Measure_Weight::TON);
    ?>

### 18.4.2. Add and subtract

Measurements can be added together using `add()` and subtracted using `sub()`. Each addition will create a new object for the result. The actual object will never be changed by the class. The new object will be of the same type as the originating object. Dynamic objects support a fluid style of programming, where complex sequences of operations can be nested without risk of side-effects altering the input objects.

例 18.11. Adding units

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Length');

    // Define objects
    $unit = new Zend_Measure(200,Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure(1,Zend_Measure_Length::METER);

    // Add $unit2 to $unit
    $sum = $unit->add($unit2);

    echo $sum;
    ?>

| Automatic conversion
Adding one object to another will automatically convert it to the correct unit. It is not neccessary to call `convertTo()` before adding different units.

例 18.12. Subtract

Subtraction of measurements works just like addition.

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Length');

    // Define objects
    $unit = new Zend_Measure(200,Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure(1,Zend_Measure_Length::METER);

    // Subtract $unit2 from $unit
    $sum = $unit->sub($unit2);

    echo $sum;
    ?>

### 18.4.3. Compare

Measurements can also be compared, but without automatic unit conversion. Thus, `equals()` returns `TRUE`, only if both the value and the unit of measure are identical.

例 18.13. Different measurements

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Length');

    // Define measurements
    $unit = new Zend_Measure(100,Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure(1,Zend_Measure_Length::METER);

    if ($unit->equals($unit2)) {
        print "Both measurements are identical";
    } else {
        print "These are different measurements";
    }
    ?>

例 18.14. Identical measurements

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Length');

    // Define measurements
    $unit = new Zend_Measure(100,Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure(1,Zend_Measure_Length::METER);

    $unit2->setType(Zend_Measure_Length::CENTIMETER);

    if ($unit->equals($unit2)) {
        print "Both measurements are identical";
    } else {
        print "These are different measurements";
    }
    ?>

### 18.4.4. Calculate differences

To determine if a measurement is less than or greater than another, use `compare()`, which returns the difference between two objects as an integer value. Identical measurements will return 0. Lesser ones will return a negative, greater ones a positive value.

例 18.15. Difference

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Length');

    $unit = new Zend_Measure(100,Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure(1,Zend_Measure_Length::METER);
    $unit3 = new Zend_Measure(1.2,Zend_Measure_Length::METER);

    print "Equal:".$unit2->compare($unit);
    print "Greater:".$unit2->compare($unit3);
    print "Lesser:".$unit3->compare($unit2);
    ?>

### 18.4.5. Manually change values

To change the value of a measurement explicitly, use `setValue()`. to overwrite the current value. The parameters are the same as the constructor.

例 18.16. Changing a value

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Length');

    $locale = new Zend_Locale('de_AT');
    $unit = new Zend_Measure(1,Zend_Measure_Length::METER);

    $unit->setValue(1.2);
    echo $unit;

    $unit->setValue(1.2,Zend_Measure_Length::KILOMETER);
    echo $unit;

    $unit->setValue("1.234,56",Zend_Measure_Length::MILLIMETER,$locale);
    echo $unit;
    ?>

### 18.4.6. Manually change types

To change the type of a measurement without altering its value use `setType()`.

例 18.17. Changing the type

    <?php
    require_once 'Zend.php';
    Zend::loadClass('Zend_Measure');
    Zend::loadClass('Zend_Measure_Length');

    $unit = new Zend_Measure(1,Zend_Measure_Length::METER);

    // output: 1 m
    echo $unit;

    // output: 1 km
    $unit->setType(Zend_Measure_Length::KILOMETER);
    echo $unit;
    ?>

上一页 | 上一级 | 下一页
18.3. Outputting measurements | 起始页 | 18.5. Special functions