granam/strict-float

This package is abandoned and no longer maintained. The author suggests using the granam/float package instead.

Base for lightweight float containers with strict checks

2.1.1 2015-09-10 11:51 UTC

This package is not auto-updated.

Last update: 2016-03-17 21:03:42 UTC


README

Build Status

PHP does not provide scalar type hinting yet (planned for PHP 7.0).

For that reason, if we want to be sure about scalar type, a type-checking class is the only chance.

Note: requires PHP 5.4+

<?php
use Granam\Strict\Float\StrictFloat;
use Granam\Strict\Float\Exceptions\WrongParameterType;

$float = new StrictFloat(123.456);

// double(123.456)
var_dump($float->getValue());
// string(7) "123.456"
var_dump((string)$float);

$float = new StrictFloat(null, false /* explicitly non-strict*/);
// double(0)
var_dump($float->getValue());
// string(0)
var_dump((string)$float);

try {
  new StrictFloat(null);
} catch (WrongParameterType $floatException) {
  // Something get wrong: On strict mode expected float only, got NULL
  die('Something get wrong: ' . $floatException->getMessage());
}