PHP code example of withinboredom / distance

1. Go to this page and download the library: Download withinboredom/distance library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

withinboredom / distance example snippets


use Withinboredom\Distance;
use Withinboredom\Distance\Unit;

$meter = Distance::from(Unit::Meters, 1);
$centimeters = Distance::from(Unit::Centimeters, 100);

echo $meter === $centimeters ? 'true' : 'false'
// outputs: true

function moveForward(Distance $distance) { /* move */ }

moveForward(Distance::from(Unit::Miles, 10));

$meter = Meters(1);

$meter = $meter->multiply(10)->add(Inches(10)); // get 10 meters and 10 inches

echo Centimeters(10) < $meter ? 'true' : 'false';
// output: true

class Robot {
    public function __construct(
        #[Field('height_in_centimeters')]
        #[TimeAs(Unit::Centimeters)]
        public Distance $height,
    ) {}
}

$serde = new SerdeCommon(handlers: new \Withinboredom\Distance\SerdeExporter());
$serde->serialize(new Robot(Meters(5)), 'json');